gpt4 book ai didi

vb.net - 我们可以在函数 vb.net 中返回两个变量值吗?

转载 作者:行者123 更新时间:2023-12-02 04:16:08 25 4
gpt4 key购买 nike

我们可以在函数 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`

最佳答案

我可以为您提供三种方法。

首先是使用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

第三种,可能也是最有趣的方法是传递返回委托(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

此方法从 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

关于vb.net - 我们可以在函数 vb.net 中返回两个变量值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33648402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com