Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to create a generic list of named tuples in Visual Basic?

Tags:

vb.net

In C# (7+) I can declare (Better naming in Tuple classes than "Item1", "Item2")

var myList = new List<(int first, int second)>();

and later on refer to items:

var a = myList[0].second;

Is there an equivalent syntax in VB.NET?

Edit From Andrew Morton's answer, the equivalent syntax is:

Dim myList = New List(Of (first As Integer, second As Integer))
Dim a = myList(0).second
like image 719
IvanH Avatar asked Mar 01 '19 10:03

IvanH


1 Answers

Yes, see Tuples (Visual Basic).

Dim q As New List(Of (EventDate As Date, Name As String, IsHoliday As Boolean))
q.Add((EventDate:=#2019-01-01#, Name:="New Year's Day", IsHoliday:=True))
q.Add((New DateTime(2019, 6, 21), "Summer solstice", False))

Console.WriteLine(q(0).Name) ''outputs "New Year's Day"
Console.WriteLine(q(1).IsHoliday) ''outputs False

(Visual Studio 2017 and later.)

like image 93
Andrew Morton Avatar answered Nov 26 '22 04:11

Andrew Morton