VB6 Tuple Equivalent?
Asked Answered
L

5

10

I'm porting some C# code to VB6 because legacy applications. I need to store a list of pairs. I don't need to do associative lookups, I just need to be able to store pairs of items.

The snippet I'm porting from looks like this:

List<KeyValuePair<string, string>> listOfPairs;

If I were to port this to C++, I'd use something like this:

std::list<std::pair<string, string> > someList;

If this were python I'd just use a list of tuples.

someList.append( ("herp", "derp") )

I'm looking for a library type, but will settle for something else if necessary. I'm trying to be LAZY and not have to write cYetAnotherTinyUtilityClass.cls to get this functionality, or fall back on the so-often-abused string manipulation.

I've tried googling around, but VB6 is not really documented well online, and a lot of what's there is, well challenged. If you've ever seen BigResource, you'll know what I mean.

Largehearted answered 31/8, 2012 at 20:31 Comment(2)
You could use Interop and simply call the C# from the VB6? Create a COM-visible DLL from the C#.Desired
Nope, I have recieveth a Directive: thou shalt not C#. Also, I got the DB access code from the engineer who designed the database. It's, ehm, not exactly fit for production.Largehearted
W
7

If its literally just for storage you can use a Type:

Public Type Tuple
    Item1 As String
    Item2 As String
End Type

Its a bit more concise than needing a class to do the storage.

The problem with Types (known more widely as UDTs) is that there are restrictions on what you can do with them. You can make an array of a UDT. You cannot make a collection of a UDT.

In terms of .Net they're most similar to Struct.

There's a walkthrough of the basics here or here.

Woodrowwoodruff answered 31/8, 2012 at 20:36 Comment(4)
I've not been using visual basic for that long, so you should explain the implications of it being treated as a primitive, because I don't know what they are. Here, have an upvote.Largehearted
Can't put UDTs in collections... well shucks. Well, I'll accept this answer even though I'm not going to use it.Largehearted
Actually, you CAN make a collection of a UDT. However, you must declare the UDT in a public class or a type libarary.Scudo
@MarkBertenshaw: Was sure you could, but couldn't get it to work (am only trying in VBA here), and online seemed to confirm it didn't work. Thx for the tip.Woodrowwoodruff
I
8

Collections of Variants can be quite flexible and unless you are really beating on them performance is not an issue:

Private Sub SomeCode()
    Dim Pair As Variant
    Dim ListOfPairs As Collection

    Set ListOfPairs = New Collection

    With ListOfPairs
        Pair = Array("this", "that")
        .Add Pair

        .Add Array("herp", "derp")

        .Add Array("weet", "tweet")

        MsgBox .Item(1)(0) 'Item index is base-1, array index base-0.

        Pair = .Item(2)
        MsgBox Pair(1)

        ReDim Pair(1)
        Pair(0) = "another"
        Pair(1) = "way"
        .Add Pair
        MsgBox .Item(4)(1)
    End With
End Sub
Ineslta answered 1/9, 2012 at 1:7 Comment(1)
+1 Arrays in Variants is the closest thing to a tuple in VB6.Desired
W
7

If its literally just for storage you can use a Type:

Public Type Tuple
    Item1 As String
    Item2 As String
End Type

Its a bit more concise than needing a class to do the storage.

The problem with Types (known more widely as UDTs) is that there are restrictions on what you can do with them. You can make an array of a UDT. You cannot make a collection of a UDT.

In terms of .Net they're most similar to Struct.

There's a walkthrough of the basics here or here.

Woodrowwoodruff answered 31/8, 2012 at 20:36 Comment(4)
I've not been using visual basic for that long, so you should explain the implications of it being treated as a primitive, because I don't know what they are. Here, have an upvote.Largehearted
Can't put UDTs in collections... well shucks. Well, I'll accept this answer even though I'm not going to use it.Largehearted
Actually, you CAN make a collection of a UDT. However, you must declare the UDT in a public class or a type libarary.Scudo
@MarkBertenshaw: Was sure you could, but couldn't get it to work (am only trying in VBA here), and online seemed to confirm it didn't work. Thx for the tip.Woodrowwoodruff
C
2

I had a similar scenario and used Dictionary by including a reference to Microsoft Scripting Runtime library in my VB6 project. This was suggested by a colleague of mine and worked really well.

Dim dictionary As New Dictionary
Dim index As Integer

dictionary.Add "Index1", "value for first index"
dictionary.Add "Index2", "value for second index"


'To get the value for a key
Debug.Print dictionary("Key1")

'To get the value for all keys
For index = 0 To UBound(dictionary.Keys)
    Debug.Print dictionary.Keys(index) & "=" & dictionary(dictionary.Keys(index))
Next index
Cosignatory answered 15/1, 2021 at 9:21 Comment(1)
For Each Key In dict.Keys MsgBox Key & " = " & dict(Key) NextPuca
N
0

List Class? (see VB section): http://msdn.microsoft.com/en-us/library/6sh2ey19#Y0

Dictionary Class? http://msdn.microsoft.com/en-us/library/xfhwa508

Neisse answered 31/8, 2012 at 20:38 Comment(3)
I don't really want to use a list of lists (each sublist being exactly 2 elements long). It certainly won't do my old, slow codebase any favors. I'd use a dictionary but they're not mutable. Or at least they aren't in any other language I use. CMIIWLargehearted
Oh, also, keys are not unique, only key/value pairs need be unique.Largehearted
Furthermore, I'm not using VB.NET.Largehearted
B
0

You could use a Collection

dim c as new collection
c.add "a", "b"
Brett answered 31/8, 2012 at 20:38 Comment(1)
I don't want to nest collections.Largehearted

© 2022 - 2024 — McMap. All rights reserved.