0

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)
          {
          }
      }
  }
2
  • could you please include three example lists? Commented Oct 3, 2015 at 7:09
  • @un-lucky:string[] itmnme= new string[0] { }; string[] price= new string[0] { }; string[] qntity= new string[0] { }; Commented Oct 3, 2015 at 7:12

3 Answers 3

1

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; 
        }
Sign up to request clarification or add additional context in comments.

Comments

0

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
}

Comments

0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.