Question Search an array of Structs using a string as an index.

Sivvy

Member
Joined
Apr 1, 2009
Messages
10
Programming Experience
3-5
I have been searching for a while now, but I still can't seem to figure this out. I wrote a rather large program, and to make things more efficient I started to create structs to store more info in a variable.

All was well replacing my old code with new code, till I reached a certain point and found that blindly using a loop of integers to access the indexes of the array wouldn't work anymore... I need to access a specific struct by the information contained in a single variable within the struct (like a hashtable key), but can't seem to figure out how.

Is it possible for me to go through my array indexes in a fashion like this?:
VB.NET:
Dim myArray(TableCount) As myStruct
Dim TableName As String = "SomeTable"
...
Dim NewVar As String = myArray([I][B]TableName[/B][/I]).Info

If it helps any... My struct looks like this:
VB.NET:
    Structure Table
        Dim Name As String
        Dim VRC As Byte
        Dim DescLength As Byte
        Dim Description As String
        Public Sub New(ByVal TableName As String, ByVal Derived As Boolean)
            Name = TableName
            If Not Derived Then
                VRC = [DataBaseQueries].GetTableVRC(Name)
                DescLength = [DataBaseQueries].GetDescLength(Name, VRC)
                Description = [DataBaseQueries].GetTableDescription(Name, VRC, DescLength)
            Else
                VRC = Nothing
                DescLength = Nothing
                Description = Nothing
            End If
        End Sub
    End Structure
 
Rather than using an Array, can you use a dictionary?
VB.NET:
Dim myArray As Dictonary(Of String, myStruct)
Dim TableName As String = "SomeTable"
...
Dim NewVar As String = myArray(TableName).Info
 
Thank you for the very quick response, but I already have that variable being used in loops that access the indexes through integers. I could make two variables for it, though I would prefer one.

I'll use the Dictionary idea for now, though is there another way? I'm sure I've seen it before... Using either an integer or a string to access indexes.

Thank you again for the idea to hold me over till I can figure out the other way.
 
Back
Top