Create List object from another List using C# and VB.Net in WPF Application

Last Reply one year ago By pandeyism

Posted one year ago

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,

How can I enter these properties into my list of my object? Here is what I have and some ways I have been trying.

    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)
            customer = c;
            listProductNames = lpn;
            listProductPrices = lpp;
            listProductSalesTax = lpt;
            listProductQuantity = lpq;
            CreateCart(listProductNames, listProductPrices, listProductSalesTax, listProductQuantity);

        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;


Posted one year ago Modified on one year ago

Hey bigbear,

Please refer below sample.



private List<Product> listOfP = new List<Product>();
public List<Product> Cart = new List<Product>();
public Form1()
    List<Product> listprod = new List<Product>();
    listprod.Add(new Product { ProductName = "p1", ProductPrice = 50, ProductQuantity = 10, ProductSalesTax = 5 });
    listprod.Add(new Product { ProductName = "p2", ProductPrice = 70, ProductQuantity = 15, ProductSalesTax = 8 });
    listOfP = listprod;

public void ShoppingCart(List<Product> listprod)

private void CreateCart(List<Product> lcart)
    foreach (Product product in lcart)

public class Product
    public string ProductName { get; set; }
    public decimal ProductPrice { get; set; }
    public decimal ProductSalesTax { get; set; }
    public int ProductQuantity { get; set; }


Private listOfP As List(Of Product) = New List(Of Product)()
Public Cart As List(Of Product) = New List(Of Product)()
Public Sub New()
    Dim listprod As List(Of Product) = New List(Of Product)()
    listprod.Add(New Product With {
        .ProductName = "p1",
        .ProductPrice = 50,
        .ProductQuantity = 10,
        .ProductSalesTax = 5
    listprod.Add(New Product With {
        .ProductName = "p2",
        .ProductPrice = 70,
        .ProductQuantity = 15,
        .ProductSalesTax = 8
    listOfP = listprod
End Sub

Public Sub ShoppingCart(ByVal listprod As List(Of Product))
End Sub

Private Sub CreateCart(ByVal lcart As List(Of Product))
    For Each product As Product In lcart
End Sub

Public Class Product
    Public Property ProductName As String
    Public Property ProductPrice As Decimal
    Public Property ProductSalesTax As Decimal
    Public Property ProductQuantity As Integer
End Class