Tuples in Visual Basic
The new tuple syntax that was added to C# in C#7 has made my code cleaner and more expressive. It isn’t a feature that I use every day. But when there are multiple values that need to be moved as a cohesive group, it is very nice. And it is a big upgrade over the older generic tuple that always had its members named Item1 and Item2. While the generic tuple was useful for creating lightweight groupings, it hurt the readability of the code when the items had to be referenced. The new tuples can have custom names, meaning you get the benefits of tuples without sacrificing readability.
While I spend most of my professional development time in C#, I maintain multiple apps written in VB.Net. While the VB team has made a strategic decision to make their language more stable, they did add support for tuples in the most recent version. I used VB tuples for the first time recently and I am impressed with how clean the syntax is.
Declaring a Tuple
To declare a tuple, all you need to do is wrap two or more values in parentheses. In the following example, the code on line 2 is creating a tuple containing 42 and 84. Using this syntax, the items are still named “Item1” and “Item2”.
1 2 3 4 5
Function UseATuple() As Integer Dim pair = (42, 84) Return pair.Item1 End Function
The preferred way to declare tuples requires a bit more syntax, but it allows you to specify the names of the members. Using the colon equals syntax, line 2 is declaring a tuple with the same values as above, by now the items can be referenced with their identifiers, Magnitude and Direction. Line 4 uses one of the identifiers. The piece that you can’t see in a code sample is that the Visual Studio Intellisense is aware of the identifiers and will list them as autocompletion options.
1 2 3 4 5
Function UseANamedTuple() As Integer Dim vector = (Magnitude:=42, Direction:=84) Return vector.Magnitude End Function
The most common use case for tuples is returning multiple values from a function. Without Tuples, you would have to decide between returning one of the values as an out parameter or creating a small data structure to contain them. To return a tuple for a function in VB, all you have to do is specify the members of the tuple in the method signature, again wrapping them in parentheses. The names are actually optional, but I would strongly recommend giving each member a name.
1 2 3
Function ReturnATuple(value As Integer) As (passed As Boolean, result As Integer) Return (True, value) End Function
If you need to pass a tuple to a function, the syntax looks similar to the other cases. The tuple declaration is in a parameter. The only difference is that you do have to specify an identifier for the tuple.
1 2 3 4 5 6 7
Function PassATuple(pair As (isValid As Boolean, value As Integer)) As Integer If pair.isValid Then Return pair.value Else Return 0 End If End Function
Feel free to comment on the post but keep it clean and on topic.comments powered by Disqus
My name is Eric Potter. I have an amazing wife and 5 wonderful children. I am a Microsoft MVP for Developer Tools and Technologies, the Director of Technical Education for Sweetwater in Ft. Wayne Indiana, and an adjunct professor for Indiana Tech. I am a humble toolsmith.