Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

class DataBase
    {
        public string Name { get;  set; }
        public double Price { get;  set; }
        public double Quantity { get;  set; }
    }



 public static void GetProdInfo(List<DataBase> dataInfo)
            {
                foreach(var info in dataInfo)
                {
                    Console.WriteLine(info);
                }
            }

why i am not getting the data from DataBase like name price quant etc, i'm getting MenuDatabaseUpdated.DataBase something like that, help me please, how do i print the components of DataBase? <3 i've tried to write Object instead of var but the answer is the same :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

It depends on what do you want to print.

At this line:

Console.WriteLine(info);

info is an instance of class Database. When Writeline tries to print "info", or an object of some class in general, it calls ToString() method, which by default is the full name of your class. So, you have to override ToString method and create the string representation of your class.

Something like this:

class DataBase
{
    public string Name { get;  set; }
    public double Price { get;  set; }
    public double Quantity { get;  set; }

    public override string ToString()
    {
         return $"Name: {Name} Price: {Price} Quantity: {Quantity}";
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...