Reputation: 185
Iam using 3 string arrays.i want to get each item corresponds to the index,ie if itnme will have the second item,then i need to get second item from other two string arrays such as qntity,price.How can i possible this?
string[] itmnme= new string[0] { };
string[] price= new string[0] { };
string[] qntity= new string[0] { };
foreach (string iname in itmnme)
{
foreach (string qnt in qntity)
{
foreach (string prc in price)
{
}
}
}
Upvotes: 0
Views: 2000
Reputation: 90
Sounds like you will need to retrieve data from each array, given a position, is that right? If so you can have a method like:
public string[] getItemAtPosition(int position)
{
string[] dataResult = new string[0]{};
string itmnmeItem = itmnme[position];
string qntityItem = qntity[position];
string priceItem = price[position];
dataResult.add(itmnmeItem);
dataResult.add(qntityItem);
dataResult.add(priceItem);
return dataResult;
//now this array contains data from each array at the specified position
}
Upvotes: 0
Reputation: 29026
I think the best way that you can adopt is, using List Of Classes:
public class Product
{
public string itmnme;
public string price;
public string qntity;
}
Your List will be like :
List<Product> productList = new List<Product>{new Product()
{itmnme="item1",price="20",qntity="1"},
new Product(){itmnme="item2",price="220",qntity="3"} };
So that you can Iterate the List and easily get value from the List:
foreach (var item in productList)
{
string itmnme=item.itmnme;
string price = item.price;
string qntity=item.qntity;
}
Upvotes: 1
Reputation: 24901
Assuming the size of all your arrays is identical you can use such code:
for(int i=0; i<itmnme.Length; i++)
{
var name = itmnme[i];
var quantity = qntity[i];
var price = price[i];
// do what you need with these values
}
Upvotes: 0