Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,
I have a C# WPF window that I insert 4 List<t> a string,decimal,decimal, and int. Each list is numbered correctly 0,1,2… with the correct information in order in the list. I am trying to create a public property of a List<myobject> that has properties myobject.string, myobject.decimal, myobject.decimal, myobject.int How can I enter these properties into my list of my object? Here is what I have and some ways I have been trying.

C#
public partial class ShoppingCart : Window
    {
        private List<string> listProductNames = new List<string>();
        private List<decimal> listProductPrices = new List<decimal>();
        private List<decimal> listProductSalesTax = new List<decimal>();
        private List<int> listProductQuantity = new List<int>();
        public List<Product> Cart => new List<Product>();

        public ShoppingCart(Customer c, List<string> lpn, List<decimal> lpp, List<decimal> lpt, List<int> lpq)
        {
            InitializeComponent();
            customer = c;
            listProductNames = lpn;
            listProductPrices = lpp;
            listProductSalesTax = lpt;
            listProductQuantity = lpq;
            CreateCart(listProductNames, listProductPrices, listProductSalesTax, listProductQuantity);
            PopulateListViewCart();
        }

        private void CreateCart(List<string> lpn, List<decimal> lpp, List<decimal> lpt, List<int> lpq)
        {
            selectedProduct = new Product();

            foreach(string name in lpn)
            {
                selectedProduct.ProductName = name;
            }
            foreach(decimal price in lpp)
            {
                selectedProduct.ProductPrice = price;
            }
            foreach(decimal tax in lpt)
            {
                selectedProduct.ProductTax = tax;
            }
            foreach(int quantity in lpq)
            {
                selectedProduct.ProductQuantity = quantity;
            }
            Cart.Add(selectedProduct);
        }
}


What I have tried:

foreachs setting prod seperately and adding to list
Posted
Updated 6-Dec-18 7:02am

Have you tried using the indexes and matching them with each other? e.g.

C#
for (int i = 0; i < lpn.Count; i++)
{
    Product selectedProduct = new Product();
    selectedProduct.ProductName = lpn[i];
    selectedProduct.ProductPrice = lpp[i];
    // etc
    Cart.Add(selectedProduct);
}
 
Share this answer
 
public Product GetProduct( int n ) {

   Product p = new Product();

   p.ProductName = this.listProductNames[ n ];
   // etc.

   return p;
}
 
Share this answer
 
Comments
TheBigBearNow 6-Dec-18 17:46pm    
Hey everyone sorry for the late response. I ended up using a List<product> which worked but i only got it to be loaded in the field. I am trying to populate a property of List<product> so i can use databinding but i did the List.Add() and nothing was added to my property.
//no longer using my 4 seperate lists because all fields are in my product class.
        private List<Product> listOfP = new List<Product>();
        public List<Product> Cart => new List<Product>();

        public ShoppingCart(Customer c, List<Product> listprod)
        {
            InitializeComponent();
            customer = c;
 
            listOfP = listprod;
            CreateCart(listOfP);
            PopulateListViewCart();
        }
 
private void CreateCart(List<Product> lcart)
        {
  
            //Cart.Add(selectedProduct);
            foreach(Product product in listOfP)
            {
                Cart.Add(product);
            }
        }

This property is not being filled with my listofP but my listofP has the correct items.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900