Helpful Information
 
 
Category: Visual Basic Programming
Working with arrays

I want to maintain values in an array var like so:
1 "A","ew2","xyz"
2 "B","re3","ert"
3 "C","5ty","dfg"
4 "D","6yu","wer"

where
1,2,3,4 will be the index

I will search with a string that will match a value in the [A,B,C,D] field
If I was looking for C and it was found in the said Array field then the function should return "5ty" & "dfg". I also want to delete that particular row based on the same search field ie: [A,B,C,D]
so that the new array looks like this
1 "A","ew2","xyz"
2 "B","re3","ert"
3 "D","6yu","wer"

Any pointers on how this can be done in VB?


Note: The size of array will increase at runtime and cant be decieded at the start.


Thanks.

Would using a Dictionary object work? You would reference the Microsoft Scripting Runtime which is scrrun.dll. This is the same as a Perl associative array. Basically, given value A, return value B



Sub main()
Dim x As New Scripting.Dictionary
Dim z() As String
Dim key As Variant

x.Add "A", "ew2" & "|" & "xyz"
x.Add "B", "re3" & "|" & "ert"
x.Add "C", "5ty" & "|" & "dfg"
x.Add "D", "6yu" & "|" & "wer"

z = Split(x("C"), "|")
MsgBox "1st C Value: " & z(0)
MsgBox "2nd C Value: " & z(1)

MsgBox "Array size: " & x.Count

For Each key In x
MsgBox key & " -> " & x(key)
Next key

x.Remove ("C")

If x.Exists("C") Then
MsgBox "C exists"
Else
MsgBox "C was removed from the dictionary"
End If
End Sub

Thanks dcaillouet for the response
Theres just one problem... the size of the array remains the same.... cant the Key "C" itself be removed so that the new array reads like this

1 "A","ew2","xyz"
2 "B","re3","ert"
3 "D","6yu","wer"

The snippet of code I threw together was just to illustrate some of the methods and properties of the dictionary so you would be able to see how it worked.

This line of code removes the "C" element from the dictionary.
x.Remove ("C")Theres just one problem... the size of the array remains the same....
No. If you run this code again at the end of the routine, you will see that the count is now 3 instead of 4 and that the "C" element is missing.
MsgBox "Array size: " & x.Count

For Each key In x
MsgBox key & " -> " & x(key)
Next key










privacy (GDPR)