none
How to Create Dynamic Multi-Dimensional Arrays RRS feed

  • Question

  • Hi,

    How do I declare a multi-dimensional array whose size is unknown at the start of the application?  For example, depending on the state and county in the US, I need to declare a multi-dimensional array where each member is also  an array consisting of 3 fields.

    Sample data:
    state = Delaware
    number of counties = 3
    array should be: 
       arr = [{"Kent", "Delaware", "10001"}, {"New Castle", "Delaware", "10003"}, {"Sussex", "Delaware", "10005"}]

    Now, when it gets to Texas,
    state = Texas
    number of counties = 254

    So, how do I declare an array that can accommodate both sizes?

    Appreciate any help.  I'm developing a Windows Form application using Visual Studio 2015.


    Marilyn Gambone

    Monday, May 6, 2019 6:49 PM

Answers

  • Your first step should be to create a class to hold those three fields, rather than using an array of three strings.

    public class County
    {
       public string Name{get; set;} // example, "New Castle".
       public string State{get; set;} // example, "Delaware". 
       public string Code{get; set;} // example, "10003".
    
       public County(string name, string state, string code)
       {
          Name = name;
          State = state;
          Code = code;
       }
    }

    Then use an array of County objects. Or probably better, a List<County>.

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8

    • Marked as answer by deskcheck1 Tuesday, May 7, 2019 9:16 AM
    Tuesday, May 7, 2019 2:23 AM
  • It is better to do C# list of lists for dynamic 2d array.

    Example:

    List<List<int>>x=new List<List<int>>();
    List<int>y=new List<int>();

     y.Add(1);
     y.Add(2);
     y.Add(3);
     x.Add(y);

    Now we can access it like a 2d array.


    • Edited by Evan Chatter Thursday, December 19, 2019 3:43 AM NA
    • Marked as answer by deskcheck1 Friday, December 20, 2019 2:10 PM
    Thursday, December 19, 2019 3:43 AM

All replies

  • Hello deskcheck1,

    This forum is for "Discuss general issues about developing applications for Windows." It is win32 C++ focused.

    Since this issue is about C# programming I'll move it to C# forum for more professional support.

    Best regards,

    Rita


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 7, 2019 2:02 AM
  • Your first step should be to create a class to hold those three fields, rather than using an array of three strings.

    public class County
    {
       public string Name{get; set;} // example, "New Castle".
       public string State{get; set;} // example, "Delaware". 
       public string Code{get; set;} // example, "10003".
    
       public County(string name, string state, string code)
       {
          Name = name;
          State = state;
          Code = code;
       }
    }

    Then use an array of County objects. Or probably better, a List<County>.

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8

    • Marked as answer by deskcheck1 Tuesday, May 7, 2019 9:16 AM
    Tuesday, May 7, 2019 2:23 AM
  • Hi deskcheck1,

    Thank you for posting here.

    You can try the another ways to declare an array that can accommodate both sizes.

    1,  Direct assignment when declaring an array. For example:
    string[,] array =  new string[,]{  {"Kent", "Delaware", "10001"}, {"New Castle", "Delaware", "10003"}, {"Sussex", "Delaware", "10005"} };
    
    string[,] array =  new string[,]{  {"Kent", "Delaware", "10001"}, {"New Castle", "Delaware", "10003"}, {"Sussex", "Delaware", "10005"},  {"Kent", "Delaware", "10001"}, {"New Castle", "Delaware", "10003"}, {"Sussex", "Delaware", "10005"} };
    2,Declared a jagged array. Such kind of arrays can have different sizes for all dimensions. I am not sure the Texas has the same format with Delaware, if not, you could use the code below.
    string[][] jaggedStrings =  {
    new string[] {"x","y","z"},
    new string[] {"x","y"},
    new string[] {"x"}
    };

    If yes, this still works.

    string[][] jaggedStrings =  {
    new string[] {"x","y","z"},
    new string[] {"x","y","z"},
    new string[] {"x","y","z"}
    };

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 7, 2019 7:20 AM
    Moderator
  • Thanks.  This worked best for me.  Appreciate it.

    Marilyn Gambone

    Tuesday, May 7, 2019 9:17 AM
  • It is better to do C# list of lists for dynamic 2d array.

    Example:

    List<List<int>>x=new List<List<int>>();
    List<int>y=new List<int>();

     y.Add(1);
     y.Add(2);
     y.Add(3);
     x.Add(y);

    Now we can access it like a 2d array.


    • Edited by Evan Chatter Thursday, December 19, 2019 3:43 AM NA
    • Marked as answer by deskcheck1 Friday, December 20, 2019 2:10 PM
    Thursday, December 19, 2019 3:43 AM
  • Hi Evan,

    I haven't tried this.  But it makes sense.  Will try it next time I need a multi-dimensional array.  Thanks.

    Marilyn


    Marilyn Gambone

    Friday, December 20, 2019 2:10 PM
  • Nice code suggestion, thanks
    Friday, December 20, 2019 3:58 PM