Two Dimensional Dynamic Array in VB.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yahya30
    New Member
    • May 2007
    • 10

    Two Dimensional Dynamic Array in VB.net

    Hi all,
    I'm looking for a way to create two dimentional dynamic array of type object in vb.net desktop application.

    I know how to create a normal dynamic array:-

    Dim MyArr As New System.Collecti ons.ArrayList
    MyArr.Add("Name ")
    MyArr.Add("Age" )

    I'll be waiting for your reply.

    Kind regards,
    Yahya
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I don't understand your question? How is a normal array not 2 dimensional?

    Comment

    • TRScheel
      Recognized Expert Contributor
      • Apr 2007
      • 638

      #3
      Originally posted by yahya30
      Hi all,
      I'm looking for a way to create two dimentional dynamic array of type object in vb.net desktop application.

      I know how to create a normal dynamic array:-

      Dim MyArr As New System.Collecti ons.ArrayList
      MyArr.Add("Name ")
      MyArr.Add("Age" )

      I'll be waiting for your reply.

      Kind regards,
      Yahya
      So you are looking to make an array of arrays?

      [CODE="vb"]Dim MyArr As New System.Collecti ons.ArrayList
      Dim ChildArr as New System.Collecti ons.ArrayList
      MyArr.Add(Child Arr)[/CODE]

      Shouldn't that work?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        A normal array is single-dimensioned (don't think math, thing coding)
        Single dimension (x)
        [0][1][2][3][4][5][6][7][8][9]

        Two-dimensions (x,y)
        [0,0][1,0][2,0][3,0][4,0]
        [0,1][1,1][2,1][3,1][4,1]

        you could make an arraylist of arraylist, but I think there might be better alternatives

        Comment

        • TRScheel
          Recognized Expert Contributor
          • Apr 2007
          • 638

          #5
          Originally posted by Plater
          A normal array is single-dimensioned (don't think math, thing coding)
          Single dimension (x)
          [0][1][2][3][4][5][6][7][8][9]

          Two-dimensions (x,y)
          [0,0][1,0][2,0][3,0][4,0]
          [0,1][1,1][2,1][3,1][4,1]

          you could make an arraylist of arraylist, but I think there might be better alternatives
          Indeed there are...

          An array of objects is almost always a better choice (ie, an array of a struct or class).

          Comment

          • yahya30
            New Member
            • May 2007
            • 10

            #6
            Originally posted by TRScheel
            Indeed there are...

            An array of objects is almost always a better choice (ie, an array of a struct or class).
            I mean by normal array: - The single dimentional array as you know.

            No one of you has provided me with a solution for this problem till now, you suggested to make an array of arrays, but how this could be done by code.

            You have replied saying that making an array of arrays is the solution by doing this:-

            Dim MyArr As New System.Collecti ons.ArrayList
            Dim ChildArr as New System.Collecti ons.ArrayList
            MyArr.Add(Child Arr) ' Please note that it should be MyArr.AddRange( ChildArr) because you are adding an object that contains a list of items, you are not adding just a single item. Anyhow I'm not convinced by this solution so far, so could you please clarify your point of view.


            Kind regards,
            Yahya

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              You want something addressable like:
              myarrayobject[0][2].ToString();
              Or something like that right? I am pretty sure there are objects in the System.Collecti on and System.Collecti on.Specialized that will handle that.

              Comment

              • SammyB
                Recognized Expert Contributor
                • Mar 2007
                • 807

                #8
                Originally posted by yahya30
                Hi all,
                I'm looking for a way to create two dimentional dynamic array of type object in vb.net desktop application.

                I know how to create a normal dynamic array:-

                Dim MyArr As New System.Collecti ons.ArrayList
                MyArr.Add("Name ")
                MyArr.Add("Age" )

                I'll be waiting for your reply.

                Kind regards,
                Yahya
                What VB are you using, 2003 or 2005? There is a lot of overhead with ArrayLists, if you can use 2005, generics is a much better option. What are you trying to do? If you describe the problem, we may be able to devise a good data structure for you.

                Comment

                • yahya30
                  New Member
                  • May 2007
                  • 10

                  #9
                  Originally posted by TRScheel
                  So you are looking to make an array of arrays?

                  [CODE="vb"]Dim MyArr As New System.Collecti ons.ArrayList
                  Dim ChildArr as New System.Collecti ons.ArrayList
                  MyArr.Add(Child Arr)[/CODE]

                  Shouldn't that work?

                  Yes I got you now, I did understand your code snippet. To clarify your description I'll write:-

                  Dim MyArr As New System.Collecti ons.ArrayList
                  Dim ChildArr as New System.Collecti ons.ArrayList
                  MyArr.Add(Child Arr) 'This will add the array "ChildArr" to the first row of "MyArr". Therfore if ChildArr contains the following items: C1,C2,C3 then "MyArr" will contain C1,C2,C3 in its first row.

                  Now if you want to add another items to the second row of "MyArr", then you should define another array for say "ChildArr2" and add it to "MyArr" by writing: -

                  Dim ChildArr2 as New System.Collecti ons.ArrayList
                  ChildArr2.Add(" CA1")
                  ChildArr2.Add(" CA2")
                  ChildArr2.Add(" CA3")
                  MyArr.Add(Child Arr2) ' This will add the array of "ChildArr2" into the second row in "MyArr", so if "ChildArr2" contains the following items: CA1, CA2, CA3 then "MyArr" will contain CA1, CA2, CA3 in its second row.

                  This means that you will get a two dimensional dynamic array of type object called "MyArr" that has two rows and three columns with the data of : -

                  C1, C2, C3
                  CA1, CA2, CA3

                  Therefore for each row you should define a new dynamic array to be added to that row ----- etc.

                  This is what you mean, isn't it?

                  Note: For the guy asking about my .net version, I'm using 2003

                  Thank you Mr. TRScheel for your help.


                  Kind regards,
                  Yahya

                  Comment

                  Working...