-1
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 :(

  • 8
    Because your `class` doesn't override `ToString`: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method – UnholySheep Mar 20 '19 at 21:31
  • 3
    Possible duplicate of [How do I automatically display all properties of a class and their values in a string?](https://stackoverflow.com/questions/4023462/how-do-i-automatically-display-all-properties-of-a-class-and-their-values-in-a-s) – mjwills Mar 20 '19 at 21:31
  • 4
    Also, it's a bad practice to use `double` as a price. Use `decimal` to represent money quantities. – Eric Lippert Mar 20 '19 at 21:32
  • In the context of a product database, I would also consider representing quantity as a decimal. Usually fractional product quantity purchases are exact (i.e., even if the received product is measured, the invoice quantity is considered exact for purposes of accounting and for purposes of converting between price per unit and total price). – Brian Mar 21 '19 at 13:13

1 Answers1

1

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}";
    }
}
Txaran
  • 154
  • 2
  • 13