我们可以在函数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`
答案 0 :(得分:2)
我可以为您提供三种方式。
首先使用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
第二种是定义你自己的回归类。
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
第三种,也许最有趣的方式是传递一个返回委托。
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
此方法从Function
更改为Sub
,[return] As Action(Of Integer, Integer)
允许多对的多个返回值。
你甚至可以将它与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
答案 1 :(得分:1)
根据我的理解,你希望函数返回锯齿数组中值的第一个外观的索引。
一种方法是返回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
答案 2 :(得分:1)
以下是两种方法:
第一种也是最简单的方法是将函数更改为子程序,如下所示:
Public Sub jaggedarr(ByRef arr()() As Integer, ByVal keyvalue As Integer, ByRef I as Integer, ByRef J as Integer)
并且在子例程中,在for循环之前将j和i初始化为-1, 并且你当前有return语句的地方只需添加一个Exit For
第二种方法如下:
在函数中返回值如下:
Return i.tostring & "|" & j.tostring
并在调用程序中
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)