我们可以在函数 vb.net 中返回两个变量值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33648402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we return two variable value in function vb.net?
提问by Ahad Murtaza
can we return two values in function vb.net
我们可以在函数 vb.net 中返回两个值吗
Public Function jaggedarr(ByRef arr()() As Integer, ByVal keyvalue As Integer) As String
For i As Integer = 0 To arr.GetUpperBound(0)
For j As Integer = 0 To arr(i).GetUpperBound(0)
If arr(i)(j) = keyvalue Then
Return i j
End If
Next
Next
End Function`
回答by Enigmativity
I can offer you three ways of doing it.
我可以为您提供三种方法。
First is to use Tuple(Of Integer, Integer).
首先是使用Tuple(Of Integer, Integer).
Public Function jaggedarr(ByRef arr()() As Integer, ByVal keyvalue As Integer) As Tuple(Of Integer, Integer)
For i As Integer = 0 To arr.GetUpperBound(0)
For j As Integer = 0 To arr(i).GetUpperBound(0)
If arr(i)(j) = keyvalue Then
Return Tuple.Create(i, j)
End If
Next
Next
End Function
The second is to define you own return class.
第二个是定义你自己的返回类。
Public Function jaggedarr(ByRef arr()() As Integer, ByVal keyvalue As Integer) As Pair
For i As Integer = 0 To arr.GetUpperBound(0)
For j As Integer = 0 To arr(i).GetUpperBound(0)
If arr(i)(j) = keyvalue Then
Return New Pair(i, j)
End If
Next
Next
End Function
Public NotInheritable Class Pair
Implements IEquatable(Of Pair)
Private ReadOnly _I As Integer
Private ReadOnly _J As Integer
Public ReadOnly Property I As Integer
Get
Return _I
End Get
End Property
Public ReadOnly Property J As Integer
Get
Return _J
End Get
End Property
Public Sub New(I As Integer, J As Integer)
_I = I
_J = J
End Sub
Public Overrides Function Equals(obj As Object) As Boolean
If TypeOf obj Is Pair
Return Equals(DirectCast(obj, Pair))
End If
Return False
End Function
Public Overloads Function Equals(obj As Pair) As Boolean Implements IEquatable(Of Pair).Equals
If obj Is Nothing
Return False
End If
If Not EqualityComparer(Of Integer).[Default].Equals(_I, obj._I)
Return False
End If
If Not EqualityComparer(Of Integer).[Default].Equals(_J, obj._J)
Return False
End If
Return True
End Function
Public Overrides Function GetHashCode() As Integer
Dim hash As Integer = 0
hash = hash Xor EqualityComparer(Of Integer).[Default].GetHashCode(_I)
hash = hash Xor EqualityComparer(Of Integer).[Default].GetHashCode(_J)
Return hash
End Function
Public Overrides Function ToString() As String
Return [String].Format("{{ I = {0}, J = {1} }}", _I, _J)
End Function
End Class
The third, and probably the most interesting way is to pass a return delegate.
第三种,可能也是最有趣的方式是传递返回委托。
Public Sub jaggedarr(ByRef arr()() As Integer, ByVal keyvalue As Integer, ByVal [return] As Action(Of Integer, Integer))
For i As Integer = 0 To arr.GetUpperBound(0)
For j As Integer = 0 To arr(i).GetUpperBound(0)
If arr(i)(j) = keyvalue Then
[return](i, j)
End If
Next
Next
End Sub
This method changes from a Functionto Suband the [return] As Action(Of Integer, Integer)allows multiple return values of multiple pairs.
此方法从一个Function到Sub并[return] As Action(Of Integer, Integer)允许多个对的多个返回值。
You could even couple this with the Pairclass and do this:
您甚至可以将其与Pair课程结合起来并执行以下操作:
Public Sub jaggedarr(ByRef arr()() As Integer, ByVal keyvalue As Integer, ByVal [return] As Action(Of Pair))
For i As Integer = 0 To arr.GetUpperBound(0)
For j As Integer = 0 To arr(i).GetUpperBound(0)
If arr(i)(j) = keyvalue Then
[return](New Pair(i, j))
End If
Next
Next
End Sub
回答by Rob
Here are two approaches:
这里有两种方法:
The first and simplest approach is to change your function to a subroutine as follows:
第一种也是最简单的方法是将您的函数更改为子程序,如下所示:
Public Sub jaggedarr(ByRef arr()() As Integer, ByVal keyvalue As Integer, ByRef I as Integer, ByRef J as Integer)
and in the subroutine initialize j and i to -1 before the for loop, and where you currently have the return statement just add in a Exit For
并在子例程中,在 for 循环之前将 j 和 i 初始化为 -1,并且您当前拥有 return 语句的地方只需添加 Exit For
The Second approach is as follows:
第二种方法如下:
In the function return the values as follows:
在函数中返回值如下:
Return i.tostring & "|" & j.tostring
and in the calling program
并在调用程序中
Dim ReturnValues as String = jaggedarr(...)
Dim EachReturnValue() as string = ReturnValue.split("|")
Dim FirstValue as integer = Ctype(EachReturnValue(0), Integer)
Dim SecondValue as integer = Ctype(EachReturnValue(1), Integer)
回答by Zohar Peled
From what I understand you want the function to return the indexes of the first appearance of the value in the jagged array.
One way to do it is return a Tuple:
据我了解,您希望该函数返回该值在锯齿状数组中第一次出现的索引。
一种方法是返回一个元组:
Public Function jaggedarr(ByRef arr()() As Integer, ByVal keyvalue As Integer) As Tuple(Of Int, Int)
For i As Integer = 0 To arr.GetUpperBound(0)
For j As Integer = 0 To arr(i).GetUpperBound(0)
If arr(i)(j) = keyvalue Then
Return new Tuple(i, j)
End If
Next
Next
'' if no string was found:
return new Tuple(-1,-1) '' or you can return Nothing...
End Function

