locked
Access system object array data or cast it to string array C# RRS feed

  • Question

  • Hello there, I'm sorry to ask this but I'm in a learning curve and have a little problem here. I created an Object array which is filled up as in image below. Maybe someone could help me to know how can I access this data directly to pass it to a string.  or how could I cast this 2D array to a string array which I could access in a way like UserDevices[0,1] .  I really appreciate your help

    Tuesday, August 26, 2014 12:56 AM

Answers

  • if you have to access item from second sub array(in your code it is List<string>). you can use foreach loop to go to that item. Check code given below where MainList is your "UserDevices" and "subList" is child list

    List<List<string>> MainList = new List<List<string>>(); List<string> subList = new List<string>(); for (int i = 0; i < 5; i++) { subList.Add(i.ToString() + " Item"); } MainList.Add(subList); MainList.Add(subList); MainList.Add(subList); MainList.Add(subList); // above code is only to add Dummy data foreach (List<string> subListitems in MainList) { foreach (var subitems in subListitems) { string str = subitems;

    // here you can get each item from your second list

    } }

    You can customise your code based on from which Parent you have to get child records by specifying index in Main foreach loop like

     	    foreach (var subListitems in MainList[0])
                {
                    string str = subListitems.ToString();   
                }

    OR else you can directly access Child item at particular index like

    string str = MainList[0][1].ToString();   

    Also, if you are getting string Array from database(though image you have provided show it as List<string>) you can use above methods, by converting your String array to list. like as below

    string[] arr; // Which is string array you are getting
    List<string> list = new List<string>(arr);

    Check if it helps you.

    Regards,

    Ajay



    • Edited by Ajaykumar.Thite Tuesday, August 26, 2014 7:06 AM
    • Proposed as answer by Amanda Zhu Thursday, August 28, 2014 9:30 AM
    • Marked as answer by Amanda Zhu Monday, September 1, 2014 2:12 AM
    Tuesday, August 26, 2014 6:58 AM

All replies

  • if you have to access item from second sub array(in your code it is List<string>). you can use foreach loop to go to that item. Check code given below where MainList is your "UserDevices" and "subList" is child list

    List<List<string>> MainList = new List<List<string>>(); List<string> subList = new List<string>(); for (int i = 0; i < 5; i++) { subList.Add(i.ToString() + " Item"); } MainList.Add(subList); MainList.Add(subList); MainList.Add(subList); MainList.Add(subList); // above code is only to add Dummy data foreach (List<string> subListitems in MainList) { foreach (var subitems in subListitems) { string str = subitems;

    // here you can get each item from your second list

    } }

    You can customise your code based on from which Parent you have to get child records by specifying index in Main foreach loop like

     	    foreach (var subListitems in MainList[0])
                {
                    string str = subListitems.ToString();   
                }

    OR else you can directly access Child item at particular index like

    string str = MainList[0][1].ToString();   

    Also, if you are getting string Array from database(though image you have provided show it as List<string>) you can use above methods, by converting your String array to list. like as below

    string[] arr; // Which is string array you are getting
    List<string> list = new List<string>(arr);

    Check if it helps you.

    Regards,

    Ajay



    • Edited by Ajaykumar.Thite Tuesday, August 26, 2014 7:06 AM
    • Proposed as answer by Amanda Zhu Thursday, August 28, 2014 9:30 AM
    • Marked as answer by Amanda Zhu Monday, September 1, 2014 2:12 AM
    Tuesday, August 26, 2014 6:58 AM
  • Hi Ajaykumar.Thite sorry for the late response and thanks a lot for the help

    I already checked your suggestion and first loop works ok for me.

    But also in your response below I actually have an object array with a sub list of string type.  so I get the error that says I can't get indexed string [] from an object array.  And also what I'd like to know is how to do the conversion from object[] to string[].

    OR else you can directly access Child item at particular index like

    string str = MainList[0][1].ToString();   

    Also, if you are getting string Array from database(though image you have provided show it as List<string>) you can use above methods, by converting your String array to list. like as below

    string[] arr; // Which is string array you are getting
    List<string> list 
    Wednesday, September 3, 2014 2:45 AM