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

Last Reply 6 months ago By pandeyism

Posted 6 months 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, 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.

    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);
        }
    }

 

Posted 6 months ago Modified on 6 months ago

Hey bigbear,

Please refer below sample.

Code

C#

private List<Product> listOfP = new List<Product>();
public List<Product> Cart = new List<Product>();
public Form1()
{
    InitializeComponent();
    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;
    ShoppingCart(listOfP);
}

public void ShoppingCart(List<Product> listprod)
{
    CreateCart(listprod);
}

private void CreateCart(List<Product> lcart)
{
    foreach (Product product in lcart)
    {
        Cart.Add(product);
    }
}

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

VB.Net

Private listOfP As List(Of Product) = New List(Of Product)()
Public Cart As List(Of Product) = New List(Of Product)()
Public Sub New()
    InitializeComponent()
    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
    ShoppingCart(listOfP)
End Sub

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

Private Sub CreateCart(ByVal lcart As List(Of Product))
    For Each product As Product In lcart
        Cart.Add(product)
    Next
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

Screenshot