2

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`
4
  • Console.WriteLine("Result is:" & jaggedarr(jagged, val))//here errors when i passed array Commented Nov 11, 2015 at 10:15
  • 1
    what is the jaggedarr method signature? Commented Nov 11, 2015 at 10:22
  • can we return two variable in function vb.net? Commented Nov 11, 2015 at 10:28
  • Do be careful about also checking for lower bounds when doing upper bound checks. I can write Dim x As Integer(,) = Array.CreateInstance(GetType(Integer), { 5, 6 }, { 15, 5 }) which creates an array with non-zero bounds. Commented Nov 11, 2015 at 10:53

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

Grate way to kill a fly with an elephant gun ;-) btw, c# will not compile a function that should return a value but might not (not all code paths return a value). isn't that the same with vb.net?
@ZoharPeled - I don't understand what you mean about the "elephant gun" comment? VB.NET does give a warning, but will compile.
your answers just seems a bit too much for the question. not that there's anything wrong with it.
1

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

2 Comments

nitpick, isn't it Nothing? :D
@Icepickle you are correct. It's been a long time since my vb.net days, moved to c# and never looked back :-)
1

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)

1 Comment

Using ByRef and a sub seems the most compact and easy to use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.