Home > Mobile >  I'm trying to create a price calculator app for a hardware strore
I'm trying to create a price calculator app for a hardware strore

Time:07-09

I want to add every item of the store to a list and calculate the selling price of the item using another class (which I haven't created yet) but I'm struggling to create the list so it adds the instance of the class "Item". Using .Net Core 3.1. This is my code:

PS: The class is in a new tab in VisualStudio

using System;
using System.Collections.Generic;
using System.Linq;

namespace StockPriceCalculator
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var list = new List<Tuple<string, double>>();
            Item nails = new Item("Nails", 5.5);

            list.Add(nails);
        }
    }
    public class Item
    {
        public Item(string name, double price)
        {
            this.name = name;
            this.price = price;
        }
        public string name { get; set; }
        public double price { get; set; }
    }
}

CodePudding user response:

Maybe i didnt understand you but why you dont create a list of "item"?

Like that:

         public static void Main(string[] args)
            {
                var list = new List<Item>();
                Item nails = new Item("Nails", 5.5);

                list.Add(nails);
            }
        }
        public class Item
        {
            public Item(string name, double price)
            {
                this.name = name;
                this.price = price;
            }
            public string name { get; set; }
            public double price { get; set; }
        }

CodePudding user response:

If you want a list of Item objects then create a List<Item>, not a List<Tuple<string, double>>. If you create the latter then you can only add Tuple<string, double> objects to it.

  • Related