Vb.Net assignment to a Tuple
Asked Answered
B

4

5

Does VB support assignment to a tuple? If so what is the Syntax?

Private Function GetPeStream(metadataDiagnostics As DiagnosticBag, peStreamProvider As EmitStreamProvider, metadataOnly As Boolean) As (peStream As Stream, signingStream As Stream, selectedStream As Stream)
    Return ...
End Function

Dim ret As Stream
Dim peStream as Stream
Dim signingInputStream as Stream
(peStream, signingInputStream, ret) = GetPeStream(metadataDiagnostics, peStreamProvider, metadataOnly)
Bubonocele answered 10/4, 2018 at 9:0 Comment(1)
You can do "stream = GetPeStream(...)", then stream.peStream, stream.signingStream, and stream.selectedStream will show up in the intellisense list instead of stream.Item1, Item2, and Item3. That's not technically destructuring, but it works.Bugaboo
S
4

You can use below syntax for tuples to initialize and assign in single line.

Dim x As Tuple(Of Stream, Stream, Stream)= New Tuple(of Stream,Stream,Stream)(str1,str2,str3)

Important: Make sure you have str1,str2 and str3 instantiated and assigned to values before direct assignment above

Synchroscope answered 12/9, 2019 at 10:47 Comment(0)
S
2

You can avoid using Item1, Item2, etc, using the example bellow.

Private Function GetPeStream(metadataDiagnostics As DiagnosticBag, peStreamProvider As EmitStreamProvider, metadataOnly As Boolean) As (peStream As Stream, signingStream As Stream, selectedStream As Stream)
    
    .....
    Return (processed_peStream, processed_signingStream,processed_selectedStream)
End Function


Private Function ConsumingGetPeStream()...

    'Calling the function
    Dim Your_Result_From_GetPeStream = GetPeStream(metadataDiagnostics_value, peStreamProvider_value, metadataOnly_Value)
    
    'Using returned values from function    
    Your_Result_From_GetPeStream.peStream
    Your_Result_From_GetPeStream.signingStream
    Your_Result_From_GetPeStream.selectedStream
    

End Function

A more simple version for better undesrtanding

      Public Sub Main()

                Dim ReturnFunctionWithTuple = FunctionWithTuple()

                Console.WriteLine($"Your long: {ReturnFunctionWithTuple.YourFieldLong}")
                Console.WriteLine($"Your date: {ReturnFunctionWithTuple.YourFieldDate}")
                Console.WriteLine($"Your bool: {ReturnFunctionWithTuple.YourFiedBoolean}")


            End Sub

            Public Function FunctionWithTuple() As (YourFieldLong As Long, YourFieldDate As Date, YourFiedBoolean As Boolean)

                Return (55, #01/01/2021#, False)

            End Function
Somato answered 26/3, 2021 at 10:47 Comment(2)
This syntax did not work for us. We had to use the item1/Item2 syntax. visual studio accepted your syntax but when we published we got syntax errors. I would really like to use your syntax.Serenity
Review your sintaxe, I'm adding a simple version, of it.Somato
N
1

Declare the tuple variable and the function return type with the generic syntax, so that the types line up. For example:

Public Sub GetTuple()
    Dim x As Tuple(Of String, String, Integer)
    x = GetData()
End Sub

Public Function GetData() As Tuple(Of String, String, Integer)

    Dim y = New Tuple(Of String, String, Integer)("A", "B", 27)
    Return y

End Function
Nitrobacteria answered 10/4, 2018 at 12:20 Comment(2)
I don't want to assign a returned tuple another Tuple variable, I want to assign a returned Tuple to three variables, in your example a String, String and an Integer. I am trying to take apart the returned Tuple. This works in C# using the syntax shown above.Bubonocele
I have not seen a tuple member accessed in VB.net without the explicit reference to a member. I do not think it is possible, even with the new features in VB 2017. blogs.msdn.microsoft.com/vbteam/2017/03/30/…Nitrobacteria
B
0

In order to do it you have to create a new Tuple that accepts the assignment as shown by PKing and then manually copy each variable one at a time.

Dim x As Tuple(Of Stream, Stream, Stream)
x = GetData()
Dim eStream  as Stream = x.Item1
Dim signingInputStream as Stream = x.Item2
Dim Ret as Stream = x.Item3

Of course the assignments would be in a loop and have code dealing with the types.

Bubonocele answered 22/4, 2018 at 23:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.