Question Creating an array with a variable value

Danielss

Member
Joined
Mar 22, 2010
Messages
20
Programming Experience
1-3
I'm sorry, i'm sure this has a very simple solution and i've trawled the internet pretty hard looking for it. Basically, i want to declare an array with a name that is the value of a variable. I need to do this because i want to create arrays from a text file that i'm reading.

Basically, if someone could make this work then i should be able to change it to fit my use.

------------------------------

Dim var1 As String = howdy

Dim var1() as Integer

------------------------------

In this case i'd like to be declaring an array with the name 'howdy' that stores integers. In reality i'm looping a bunch of read names and would like to create an array for each name i read- but i'm quite capable of sorting the rest.

Thanks in advance for any help!
 
It sounds like you need a custom class:
VB.NET:
Public Class Person
 Public Name As String
 Public Id As Integer 
 'more fields...
End Class

Then hold them is a List(Of T):
VB.NET:
Private MyList As New List(Of Person)
Dim p As New Person
p.Name = "foo"
p.Id = 1
MyList.Add(p)
 
Or, if you need to access them by name, you could use a Dictionary, like this:
VB.NET:
        'File: array.csv contains:
        ' Howdy, 1, 2, 4, 3, 6, 5, 9, 8, 7
        ' Hi(thar, 12, 21, 42, 37, 65, 59, 94, 86, 70)
        ' Hai!, 22, 34, 27, 69, 52, 87, 41, 64, 33
        Dim ListOfArrays As New Dictionary(Of String, List(Of Integer))
        Dim ArrayReader As FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser("F:\arrays.csv")
        ArrayReader.Delimiters = {","}
        Do Until ArrayReader.EndOfData = True
            Dim RawData As New List(Of String)
            RawData.AddRange(ArrayReader.ReadFields())
            If RawData.Count > 0 Then
                Dim Values As New List(Of Integer)
                Dim Name As String = RawData.First
                RawData.RemoveAt(0)
                For Each value As String In RawData
                    Dim ConvertedValue As Integer
                    If Integer.TryParse(value, ConvertedValue) Then
                        Values.Add(ConvertedValue)
                    End If
                Next
                ListOfArrays.Add(Name, Values)
            End If
        Loop
If you needed you could make ListOfArrays a dictionary containing custom classes, or even dictionaries.

Good luck.
 
Thanks for the responses guys...

A little more complex than i originally imagined but i'll give them a go and let you know!
 
I'm sorry, i don't think these methods are quite what i want. Or possibly my visual basic level is too low to understand them. (Latter is more likely)

Let's say i want to make a simple interface so that somebody can create an array.

So i have a text box on a form, i'll call it 'textbox1'.

Then i'll have a button with a click listener that launches the code:

---------------
Dim textbox1.Text() As Integer
---------------

Now i can see why that probably wouldn't work. I'm hoping that an array will be created that holds integers and has the name of whatever is typed into textbox1.

Trying something else i thought might work

---------------
Dim name As String
name = textbox1.Text
Dim name() As Integer
---------------

In this case i've created a variable to hold the Text value of the textbox1 and i'm hoping that an array will be created that holds integers and whose name is that same as the string data that variable 'name' is holding.

Please help?

Thanks
 
Can you explain the design and what you are trying to accomplish. There may a different layout or design. What your asking for does not make a lot of sense. What your asking for is possible I just want to make sure it's the right way to go about it - or is this just a homework assignment and you are stuck just following the directions for it?
 
It's a project for work. I've found a way around doing what i wanted to do. Instead i'm using 2 dimensions arrays and the first row is holding all the variable names that i want it to instead.

Thanks for your help
 
Back
Top