1

I have written a console Application for POS(point of sale). It was my assignment. I completed it successfully but my professor asked to do it again with structure and list. Here is my code for just structure and list, i have omitted unnecessary code of application:

namespace simplePOS
{
    struct Product
    {
        public string productName;
        public double productUnitPrice;
        public int productQty;
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> Products = new List<Product>();
            Product P;

            P.productName = Console.ReadLine();
            P.productUnitPrice = Convert.ToDouble(Console.ReadLine());
            P.productQty = Convert.ToInt32(Console.ReadLine());

            Products.Add(P);

            foreach(Product x in Products){ //This doesn't work.
                Console.WriteLine(x);
            }

            Console.ReadLine();
        }
}

Now as you can see i have created a struct and then created an list and saved object of struct "P" in list. Now my question is how can i access that object "P" saved in list to display several products in some loop, say for loop?

Mansoor Akram
  • 1,997
  • 4
  • 24
  • 40
  • so is it not working? – Ehsan Sajjad Sep 26 '14 at 11:12
  • No it isn't, x is displaying "programName.Product" in console. – Mansoor Akram Sep 26 '14 at 11:13
  • 4
    You need to override `ToString`, or explicitly print the right parts of it. I would *strongly* advise against mutable value types and public fields though. – Jon Skeet Sep 26 '14 at 11:13
  • @JonSkeet can you please explain in more detail as i am new to C#. What part of app needs to be converted to string or how to print the right parts? – Mansoor Akram Sep 26 '14 at 11:16
  • @MansoorAkram Start by reading the famous [Why are mutable structs evil](http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil) – Yuval Itzchakov Sep 26 '14 at 11:17
  • You can use format in WriteLine method if you dont want to override `ToString()` `Console.WriteLine("name: {0} price: {1} qnt: {2}", P.productName , P.productUnitPrice, P.productQty)` – Renatas M. Sep 26 '14 at 11:19
  • But won't it just display the one product i enter. Lets say i have already entered 10 product objects in list and want to display all of them in a loop, will it work then? – Mansoor Akram Sep 26 '14 at 11:22

2 Answers2

2

Override to ToString()

struct Product
{
    public string productName;
    public double productUnitPrice;
    public int productQty;
        public override string ToString()
        {
            return string.Format("Name: {0}, Price: {1}, Qty: {2}",productName,productUnitPrice,productQty);
        }
}

And keep using

foreach (Product x in Products)
{
    Console.WriteLine(x);
}

this way you can format as you want

dariogriffo
  • 4,148
  • 3
  • 17
  • 34
0
foreach(Product x in Products)
{
  Console.WriteLine(x);
}

will give you result something like:".Product" as Product is a struct. to get proper result you should use the properties defined in your Product struct See below

foreach(Product x in Products)
{
  Console.WriteLine("Product: "+x.productName);
}

So it will give you result as

Product: abc

Product: xyz

jadavparesh06
  • 926
  • 7
  • 11
  • But won't it just display the one product i enter. Lets say i have already entered 10 product and are stored as objects in list and want to display all of them in a loop, will it work then? – Mansoor Akram Sep 26 '14 at 11:24
  • Okay i checked and it also works for more than one product in list. :) – Mansoor Akram Sep 26 '14 at 11:32
  • But there is one slight problem, like you can see i have 3 attributes for each product and when i add "x.productQty" it still shows only the product name and no qty. Please help – Mansoor Akram Sep 26 '14 at 11:55
  • You have to change the logic in loop accordingly to display the appropriate result. I have given you an example how to use property of struct you can apply your logic to display the result you need. – jadavparesh06 Sep 29 '14 at 04:08