简体   繁体   English

Vb.Net 分配给元组

[英]Vb.Net assignment to a Tuple

Does VB support assignment to a tuple? VB 是否支持对元组的赋值? 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)

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重要提示:在上面的直接赋值之前,确保你已经实例化了str1str2str3并赋值给了值

You can avoid using Item1, Item2, etc, using the example bellow.您可以使用下面的示例避免使用 Item1、Item2 等。

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

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

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.为了做到这一点,您必须创建一个新的元组,它接受 PKing 所示的赋值,然后一次手动复制每个变量。

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.当然,分配将处于循环中并且具有处理类型的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM