Reputation: 5
I have a static array of string[] items as below:
public string[] stringItems = {"sItem1", "sItem2", "sItem3"}
I also have a class list the following. Each array item will need to have this class list object:
public class PriceList
{
public DateTime listDate { get; set; }
public decimal listPrice { get; set;
}
public override string ToString()
{
return String.Format("Date: {0}; Price: {1};", listDate, listPrice);
}
I set the data using the following:
dataList.Add(new PriceList() { listDate = today, listPrice = price, theVolume = volume });
Can anyone help me figure out how to set the data for each index in the array using a for loop? I think each array item will need to have it's own Prices list class, but I'm not sure how to set and call them. It might be easiest for me to set it up as a method with parameters and call that for each array item.
Thanks.
To make my question more clear, I need the following: Table
The properties of each sItem might have 100 or 100,000 list items. Each sItem will always have the same amount of list items.
At different points in my program, I need to call the sItems directly for other data points.
Upvotes: 0
Views: 1651
Reputation: 21477
public class Item {
public string Name { get;set;}
public List<PriceList> Prices {get;set;} = new List<PriceList>();
}
public string[] stringItems = {"sItem1", "sItem2", "sItem3"};
var items=stringItems.Select(x=>new Item {Name=x});
-- Adding --
items.First(i=>i.Name=="sItems1").Prices.Add(new PriceList() { ... });
Upvotes: 1
Reputation: 406
You may be better to create a class that include the item description:
public class PriceList
{
public string itemDescription { get; set; }
public DateTime listDate { get; set; }
public decimal listPrice { get; set; }
public int theVolume { get; set; }
public override string ToString()
{
return String.Format("Item: {0}; Date: {1}; Price: {2}; Volume {3};", itemDescription, listDate, listPrice, theVolume);
}
}
You could dynamically add entries to an ArrayList
(in System.Collections
) or a List<T>
):
ArrayList items = new ArrayList();
or
List<PriceList> items = new List<PriceList>();
Then you can loop through your static list
foreach (string s in stringItems)
{
PriceList pl = new PriceList () { itemDescription = s, listDate = today, listPrice = price, theVolume = volume };
items.Add (pl);
}
Both ArrayList
and List
you can reference directly at indexed positions with items[n] and you can also use foreach.
Upvotes: 0