11 Replies - 271 Views - Last Post: 31 May 2019 - 07:53 AM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Running console application on book in a list error

Posted 30 May 2019 - 11:53 PM

Hi, I made book in a list with constructors as following:

public books(string inTittle, string inAuthor)
{this.inTittle = inTittle;
this.inAuthor = inAuthor;}
}

Public class Type: books
{
public string Type;
Public Type(string inTittle, string inAuthor):
base(inTittle, inAuthor)
{Type = "literature";}}

Static void Main(string(don't findsymbol on phone)args)
List<book> boook = new List<book>();
Type mybook = new type("litterateur");
Book.Add(mybook);

Foreach (book aPart in boook)
{console.writeline (aPart);

} 


What gets written when running application is this:

"Console.App1.book+Type"


What do I need to do to get the proper writing when running application?

Is This A Good Question/Topic? 0
  • +

Replies To: Running console application on book in a list error

#2 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,097
  • Joined: 12-December 12

Re: Running console application on book in a list error

Posted 31 May 2019 - 01:29 AM

Please copy and paste your real code, do not attempt to rewrite it here. We need an accurate picture to assist.
Was This Post Helpful? 0
  • +
  • -

#3 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Running console application on book in a list error

Posted 31 May 2019 - 02:14 AM

Hi, the code I am referring to is pretty simple. I just try to make a booklist it then goes something like List<book> books = new List<book>.

I have later a class of a book that also has a constructors.

Can someone please give me an example of a correct one that I can add different books with author's and titles and later type out the names when running console application?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,097
  • Joined: 12-December 12

Re: Running console application on book in a list error

Posted 31 May 2019 - 02:52 AM

Be aware of the forum rules about just asking for code.

To repeat, if you provide some code that was at least something that could be pasted into Visual Studio you are more likely to receive assistance.

But, just guessing what you are asking, if you want to display a value of an object then the value would need to be exposed as a property of the class, or a public field, and you would use book.SomePropertyOrField to reference the value.
Was This Post Helpful? 0
  • +
  • -

#5 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Running console application on book in a list error

Posted 31 May 2019 - 03:13 AM

Here is the code.

I need to make it save my book lists also write it out when I so need

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Bok
    {
        public class Böcker
        {
    
            public string Titel;
            public string Författaren;
            private string indataTitel;
            private string indataFörfattre;

            public Böcker(string indataFörfattre)
            {
                this.indataFörfattre = indataFörfattre;
            }
            public Böcker(string indataTitel, string indataFörfattre)
            {
                this.indataTitel = indataTitel;
                this.indataFörfattre = indataFörfattre;
            }

            public static int Lenght { get; internal set; }
        }
        public class Typ : Böcker
        {
            public string Typbok;
            public Typ(string indataTitel, string indataFörfattre) :
                base(indataTitel, indataFörfattre)
            {
                Typbok = "Litteraturh";
            }
        }
        public class Författare : Böcker
        {
            public string Författaren;
            public Författare(string indataFörfattre) :
                base(indataFörfattre)
            {
                Författaren = "Författaren";
            }
        }
        public class Titel : Böcker
        {
            public string Titeln;
            public Titel(string indataTitel) :
                base(indataTitel)
            {
                Titeln = "Titels";
            }
        }
        static void Main(string[] args)
        {
            List<Titel> Boken = new List<Titel>();
            Typ minBok2 = new Typ("kurslitteratur", "litteratur");
            Författare Författaren1 = new Författare("");
            Titel Titels = new Titel("");
            Typ Typbok = new Typ("kurslitteratur", "litteratur");
            Författare Författaren = new Författare( "");
            Typ Titel = new Typ("kurslitteratur", "litteratur");
            Console.WriteLine("Boken är" + Typbok.Typbok + "" +
            Författaren1.Författaren +"" + Titels.Titeln );
            Boken.Add(Titels);

            foreach (Titel aPart in Boken)
            {
                Console.WriteLine(Titels);
            }
            Console.ReadKey();
            
        }
    }
}



everytime I run it does not show the proper information the book holds
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,097
  • Joined: 12-December 12

Re: Running console application on book in a list error

Posted 31 May 2019 - 03:28 AM

            foreach (Titel aPart in Boken)
            {
                Console.WriteLine(Titels);
            }


Within the loop it is aPart that would refer to each object instance, not Titel. The available properties to display would then be available in the intellisense list.
Was This Post Helpful? 0
  • +
  • -

#7 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Running console application on book in a list error

Posted 31 May 2019 - 03:34 AM

I know that. I previously wrote:

foreach (Titel aPart in Boken)

{

    Console.WriteLine(aPart);

}



But this is how console application types when running

"ConsoleApp1.bok+titel"

I want it to only write tittle and not what it is writing now
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,097
  • Joined: 12-December 12

Re: Running console application on book in a list error

Posted 31 May 2019 - 04:12 AM

Did you understand this part...

me said:

The available properties to display would then be available in the intellisense list.

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Running console application on book in a list error

Posted 31 May 2019 - 04:20 AM

C# is not like Javascript where collections and object contents are dumped out when you pass it to something that effectively calls ToString().

So what you need to do is to use something like Console.WriteLine(aPart.Titeln) or override the ToString() method of the Titel class.

I highly recommend that you find a book or tutorial to learn C# instead of the current shotgun approach you are taking to learning. You are missing out on some of the basics of the language and then ending up banging you head into a wall.
Was This Post Helpful? 0
  • +
  • -

#10 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Running console application on book in a list error

Posted 31 May 2019 - 04:26 AM

Yes I figured it out. I wrote apart.titeln and it works perfectly fine.

What I wonder is why I can't write in a new title and getting it saved like this

titel titels = new titel(console.readline());


?

Btw
I learn better by programming myself, books are too much info when looking for specifik info so it gets boring when needing to read many different stuff that I know before getting to know what I don't know
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,097
  • Joined: 12-December 12

Re: Running console application on book in a list error

Posted 31 May 2019 - 04:43 AM

If you are attempting to create a new Titel class instance with that line then it has a capital T.

If there are error messages when you attempt to run code then provide the details of the errors. But, again, copy and paste the ACTUAL code, even if it is a single line. For example, Console won't work without a capital C and we do not wish to guess then you have actually typed things correctly.

I agree very much with Skydiver about your shotgun approach. If you took a proper approach to learning to code the simple code you are posting would not be confusing to you.

Quote

it gets boring when needing to read many different stuff that I know before getting to know what I don't know

If you are new to programming then what you don't know is everything.
Was This Post Helpful? 0
  • +
  • -

#12 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1399
  • View blog
  • Posts: 1,989
  • Joined: 24-August 11

Re: Running console application on book in a list error

Posted 31 May 2019 - 07:53 AM

View PostRealme, on 31 May 2019 - 05:26 AM, said:

I learn better by programming myself


Obviously not.

Programming isn't ping pong - you won't just learn it by doing it. You don't necessarily need to go to school for it, but you will have to do a lot of reading if you want to get any good at it. Any good programming book or course will have plenty of coding that you can do yourself, even if you just retype the examples to get used to the syntax and structure.

With C#, you're learning quite a few different things - you're learning object-oriented programming (which is a full topic unto itself), you're learning syntax, you're learning patterns and best practices, you're learning architecture, and the list goes on. Sure, it doesn't take much to slap a few things into a console app and write words in a console window, but there is a fairly steep learning curve to progress from there if this is your first language.

The books you find may not teach in the order you want to learn, but consider that the author probably knows a whole lot more about programming in general and the language specifically than you do, and the order of topics was likely chosen for a reason. If not, then you just bought the wrong book by the wrong author and you should find another. I personally hate paper in most areas of my life, but I still buy books.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1