5

What I want is to return 2 values from the database with a function and then store the values in variables so I can work with them. This is my code.

Function Buscar_Registro(ByVal xId As Integer) As String
    Dim a, b As String

    'convertir cadena
    Dim Id As Integer
    Id = xId

    'conexión
    Dim Conexion As OleDbConnection = New OleDbConnection
    Conexion.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Visual\2000Phrases\2000 Phrases.accdb"

    'cadena SQL
    Dim CadenaSQL As String = "SELECT * FROM Data WHERE Id = " & Id

    'Adaptador
    Dim Adaptador As New OleDbDataAdapter(CadenaSQL, Conexion)

    'Data set
    Dim Ds As New DataSet

    'Llenar el Data set
    Conexion.Open()
    Adaptador.Fill(Ds)
    Conexion.Close()

    'Contar registro
    If (Ds.Tables(0).Rows.Count = 0) Then
        Return False
    Else
        a = Ds.Tables(0).Rows(0)("Nombre").ToString()
        b = Ds.Tables(0).Rows(0)("Apellido").ToString()

        Ds.Dispose()
        Return a
        Return b
        Return True
    End If


End Function


Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    Randomize()
    Dim value As Integer = CInt(Int((20 * Rnd()) + 1))
    TextBox3.Text = Buscar_Registro(value)
    TextBox4.Text = 
End Sub

I dont' know how to do it. The function returns only the value of "a" Thanks

4
  • You can return an array/list of objects. Just add the return values to the array and finally return the array/list. Commented Nov 15, 2015 at 13:54
  • 2
    Or you can create your own class or structure and make it the return type of your function. Commented Nov 15, 2015 at 13:56
  • As soon as you use the return keyword, the method is ended and the value specified is returned to the calling method....basically, the first return will get hit and everything after will be ignored. Both Farham Anam and NoAlias have the correct answer to your original question. Commented Nov 15, 2015 at 13:58
  • 1
    or you could return the first DataRow which already contains both bits of info Commented Nov 15, 2015 at 13:59

3 Answers 3

21

To return more values you need to change your function "as object"

Function Buscar_Registro(ByVal xId As Integer) As Object

and then you can put your return values into an object this way:

Return{a, b, true}

You'll get your values this way:

Dim mObj as object = Buscar_Registro(yourInteger)

you'll have:

a in mObj(0) 
b in mObj(1)
True in mObj(2)

adapt it to your needs

EDIT (message to those that downvoted):

Creating a class an using a specific Object (the one created) to make a Function able to return multiple elements is surely the best choice.

Anyway, if someone doesn't know that it's possible to use the method that I showed in my answer, he is probably not (yet) able to create a class. So I think it's better give an usable (but not perfect) answer instead of a perfect (but unusable for the one who asked) answer.

This is what I think. Anyone can think differently.

Sign up to request clarification or add additional context in comments.

1 Comment

Not sure why this got downvoted TBH. Yes, using a class is & should be the way to go, but, I can also appreciate the creativity in this answer. People forget their suggested answers are exactly that - suggestions. It's up to the OP to decide which one works best for their case. Batman, Out.
8

Your best option here is to create your own class with the data you need and return that.

Public Class Data
    Public Property Nombre As String
    Public Property Apellido As String
End Class

And then do:

Function Buscar_Registro(ByVal xId As Integer) As Data
....
    If (Ds.Tables(0).Rows.Count = 0) Then
        Return Nothing
    Else
        a = Ds.Tables(0).Rows(0)("Nombre").ToString()
        b = Ds.Tables(0).Rows(0)("Apellido").ToString()

        Ds.Dispose()
        return new Data() With {.Nombre = a, .Apellido = b}
    End If
End Function

As of VB 15 you can use ValueTuple

Function Buscar_Registro(ByVal xId As Integer) As (Nombre As String, Apellido As String)
    ....
        If (Ds.Tables(0).Rows.Count = 0) Then
            Return (Nothing, Nothing)
        Else
            a = Ds.Tables(0).Rows(0)("Nombre").ToString()
            b = Ds.Tables(0).Rows(0)("Apellido").ToString()

            Ds.Dispose()
            Return (a, b)
        End If
    End Function

3 Comments

Wow. Value tuple. Is that tuple ?
@user4951 Yes You can read more about them here: ValueTuple
The posted function above doesn't work for me, I get a syntax error "Array Bounds Cannot Appear in Type Specifiers. I'm reading on MS docs that value tuples did not make it into final version of VB15. Meanwhile, I think the format must match the one described here: dotnetperls.com/multiple-return-values-vbnet
4

I'm new to .net but wouldn't "ByRef" be the easiest way?

Function Buscar_Registro(ByVal xId As Integer, ByRef a As String, ByRef b As String)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.