VB.NET 2008 global multidimensional dynamic array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srj115
    New Member
    • Sep 2007
    • 9

    VB.NET 2008 global multidimensional dynamic array

    I have a text file containing 2 dimensions (parents,childr en). There are an indeterminite number of parents, and each parent has an indeterminite of children (ie, both dynamic). I have no problems reading the data from the file. I've tried reading the file to obtain the bounds of each dimension, but I can't seem to get ReDim to work. All I want to do is get this data into a global 2-D array, so that all functions in my class have access to it. But what can I use to store this info? I tried to use a few data types (2-D string array, 2-D array list, etc), but I received errors each time ("object reference is not set to an instance of an object", "Arrays cannot be declared with New", etc). Please help, this has been driving me nuts for years!
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Create a class that holds 2 strings, parent and child. Then use a System.Collecti ons.Generic.Lis t to make an array of them.

    Comment

    • srj115
      New Member
      • Sep 2007
      • 9

      #3
      Excellent! Thank you much.

      Comment

      • srj115
        New Member
        • Sep 2007
        • 9

        #4
        Well, I thought I had it. Instead, when I go to access the list items, each item just contains the data of the very last item. For instance, when I run the StoreFamilies Sub, the message boxes just repeat the last set of data entered. Like, (if it reiterated only 3 times) I could enter "Mabel and Jenny", "Herbert and Joey", "Frank and Bobby", but the message boxes will just repeat "Frank and Bobby". Any idea what I am doing wrong? Here's my code (simplified to express my issue):
        Code:
        Dim FamilyList As New List(Of Family)
        
        Public Class Family
           Public strParent As String
           Public strChild As String
        End Class
        
        Public Sub StoreFamilies()
           Dim famTemp As New Family
           For i As Integer = 0 to 10
              famTemp.strParent = InputBox...
              famTemp.strChild = InputBox...
              FamilyList.Add(famTemp)
           Next i
           For Each asdf in FamilyList.Items
              MsgBox(asdf.strParent & VbCrLf & asdf.Child)
           Next
        End Sub
        UPDATE: I found a solution: you must null the value in the temporary item and re-dim it after each reiteration. Does anyone have any ideas why this might be necessary?
        Last edited by srj115; Aug 21 '08, 12:32 AM. Reason: Found solution

        Comment

        • cloud255
          Recognized Expert Contributor
          • Jun 2008
          • 427

          #5
          Originally posted by srj115
          Code:
          Public Sub StoreFamilies()
             Dim famTemp As New Family
             For i As Integer = 0 to 10
                famTemp.strParent = InputBox...
                famTemp.strChild = InputBox...
                FamilyList.Add(famTemp)
             Next i

          Well you create your famTemp object. Then you loop 10 times each time assigning a new value to the same object, you only created 1 famTemp object. To store x values you need x objects.

          So as you say, declaring the object inside the loop will fix this issue.

          Comment

          • srj115
            New Member
            • Sep 2007
            • 9

            #6
            Originally posted by cloud255
            Well you create your famTemp object. Then you loop 10 times each time assigning a new value to the same object, you only created 1 famTemp object. To store x values you need x objects.

            So as you say, declaring the object inside the loop will fix this issue.
            It's always obvious when you get good advice. Thank you!

            Comment

            • cloud255
              Recognized Expert Contributor
              • Jun 2008
              • 427

              #7
              Originally posted by srj115
              It's always obvious when you get good advice. Thank you!

              happy to help.
              just a side note: i know many many people rely on the garbage collector, there are many papers on the net see here and personal experience which show that this is not the best piece of technology, you should look after all those objects and take care to delete them when you dont use them anymore.

              good luck

              Comment

              Working...