Ahad Murtaza
Ahad Murtaza

Reputation: 71

Can we return two variable value in function vb.net?

can we return two values in function 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`

Upvotes: 2

Views: 7357

Answers (3)

Rob
Rob

Reputation: 3853

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

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)

Upvotes: 1

Zohar Peled
Zohar Peled

Reputation: 82474

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

Upvotes: 1

Enigmativity
Enigmativity

Reputation: 117057

I can offer you three ways of doing it.

First is to use 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 Function to Sub and the [return] As Action(Of Integer, Integer) allows multiple return values of multiple pairs.

You could even couple this with the Pair class and do this:

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

Upvotes: 3

Related Questions