bugmagnet
bugmagnet

Reputation: 7769

How do I declare a Dictionary in VB.Net with a string key and a named Tuple as the value?

I'm declaring a data structure and loading it up with data

    Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, Object) From {
{"LET", (1, "000000001", 0)},
{"PUT", (2, "000000002", 2)},
...

What I'd like to be able to do is name the components of the ValueTuple and to a certain extent this works, viz,

    Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, Object) From {
{"LET", (Id:=1, IdString:="000000001", Arity:=0)},
{"PUT", (Id:=2, IdString:="000000002", Arity:=2)},
...

What I'm stuck on at the moment is the As New Dictionary(Of String, Object) where it would be nice to be able to put something other than Object. I've tried things like Tuple, ValueTuple and various of those with (Of but nothing seems to compile.

In my tests, because it's just Of String,Object I can't see Id, IdString and Arity, I can only see Item1, Item2 and Item3

        <Fact>
        Sub TestSub2()
            Dim FEL = Lexers.English.LexerEnglishFullWithTuple
            Dim x = FEL("EDS")
            Dim y = From el In FEL Where el.Value.Item1 = 861220001 Select el.Key
            Assert.Equal("EDS", y(0))
        End Sub

Upvotes: 1

Views: 1468

Answers (2)

Zev Spitz
Zev Spitz

Reputation: 15297

Try this:

Dim LexerEnglishFullWithTuple As New Dictionary(Of String, (Id As Integer, IdString As String, Arity As Integer)) From {
    {"LET", (1, "000000001", 0)},
    {"PUT", (2, "000000002", 2)}
}

For Each x In LexerEnglishFullWithTuple
    Dim value = x.Value
    Console.WriteLine(value.Id)
    Console.WriteLine(value.IdString)
    Console.WriteLine(value.Arity)
Next

Upvotes: 3

jmcilhinney
jmcilhinney

Reputation: 54417

You just declare the name and type of the tuple's properties like you would any other:

Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, (Id As Integer, IdString As String, Arity As Integer)) From {
    {"LET", (1, "000000001", 0)},
    {"PUT", (2, "000000002", 2)},
    '...

Upvotes: 1

Related Questions