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!
VB.NET 2008 global multidimensional dynamic array
Collapse
X
-
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. -
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
Comment
-
Originally posted by srj115Code: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
-
Originally posted by cloud255Well 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
-
Originally posted by srj115It'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 luckComment
Comment