I'm not sure whether you want all the other functionality of a C# List object (such as sorting), but if you purely want a collection of objects that you can index via some form of key then VBA's Collection object should suit you just fine.
It's perfectly possible to add class objects to a collection, like so:
myCollection.Add myClassObject, key
and to retrieve it like so:
Set myClassObject = myCollection(key)
Either by good design or chance, you've come across one of the great features of a Collection in that properties of a class object held by a collection can simply be changed using standard syntax (whereas primitive data types cannot be changed; you'd have to remove and then add the variable each time):
Set myClassObject = myCollection(key)
myClassObject.Property = "a"
If you wanted to, you could add some kind of test routine to check that the object you're adding is indeed your desired class object. In the code below, you'd have a compile error if the type were incorrect similar in spirit to that of Visual Studio. Your pseudo C# code, would therefore look something like this in VBA:
Your class object (called 'ResultLine')
Option Explicit
Public VariableThing1 As String
Public VariableThing2 As String
Your module code
Option Explicit
Private mAllTheResults As Collection
Public Sub RunMe()
Dim mode As String
Dim res As ResultLine
Dim i As Long
Dim result1 As String
Dim result2 As String
Set mAllTheResults = New Collection
'Just add some dummy data for testing.
Set res = New ResultLine
With res
.VariableThing1 = "1"
.VariableThing2 = "2"
End With
mode = "key1"
AddObject res, mode
Set res = New ResultLine
With res
.VariableThing1 = "10"
.VariableThing2 = "20"
End With
mode = "key2"
AddObject res, mode
Set res = New ResultLine
With res
.VariableThing1 = "100"
.VariableThing2 = "200"
End With
mode = "key3"
AddObject res, mode
i = 1
Do While i <= 4
mode = "key" & i 'your mode.
result1 = CStr(i) 'your found data.
result2 = CStr(i * 10) 'your other found data.
Set res = GetObjectIfExists(mode)
If Not res Is Nothing Then 'it exists
'Change the values in your object.
With res
.VariableThing1 = "changed"
.VariableThing2 = "changed"
End With
Else
Set res = New ResultLine
With res
.VariableThing1 = result1
.VariableThing2 = result2
End With
AddObject res, mode
End If
i = i + 1
Loop
End Sub
Private Sub AddObject(obj As ResultLine, Optional key As String)
If key = vbNullString Then
mAllTheResults.Add obj
Else
mAllTheResults.Add obj, key
End If
End Sub
Private Function GetObjectIfExists(key As String) As ResultLine
On Error Resume Next
Set GetObjectIfExists = mAllTheResults(key)
On Error GoTo 0
End Function
If you wanted to create a kind of generic CollectionT object in which you could specify the type of 'T' as part of your code, then you could create your own Collection class. Skeleton code for that might be:
Option Explicit
Private mTestObject As Object
Private mCollection As Collection
Public Property Set TestObject(RHS As Object)
Set mTestObject = RHS
End Property
Public Sub AddObject(obj As Object, key As String)
If mTestObject Is Nothing Then
MsgBox "Type not assigned."
Exit Sub
End If
If TypeName(obj) = TypeName(mTestObject) Then
mCollection.Add obj, key
Else
MsgBox "Incorrect object"
End If
End Sub
Public Function GetObjectIfExists(key As String)
On Error Resume Next
Set GetObjectIfExists = mCollection(key)
On Error GoTo 0
End Function
Private Sub Class_Initialize()
Set mCollection = New Collection
End Sub
So, some sample module could could look like this:
Option Explicit
Private mAllTheResults As Collection_T
Public Sub RunMe()
Dim mode As String
Dim goodObject As ResultLine
Dim wrongObject As Range
Dim i As Long
Dim result1 As String
Dim result2 As String
Set mAllTheResults = New Collection_T
Set mAllTheResults.TestObject = New ResultLine
'Add a correct type.
Set goodObject = New ResultLine
With goodObject
.VariableThing1 = "a"
.VariableThing2 = "b"
End With
mAllTheResults.AddObject goodObject, "yep"
'Add an incorrect type.
Set wrongObject = Sheet1.Range("A1")
mAllTheResults.AddObject wrongObject, "nope"
'Retrieve an item
Set goodObject = mAllTheResults.GetObjectIfExists("yep")
If Not goodObject Is Nothing Then
With goodObject
.VariableThing1 = "c"
.VariableThing2 = "d"
End With
End If
'Demonstrate the collection has retained the property change.
MsgBox "VariableThing1 = " & mAllTheResults.GetObjectIfExists("yep").VariableThing1
End Sub