Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return 2 values from function vb.net

Tags:

vb.net

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

like image 959
alex prezmon Avatar asked Nov 27 '22 20:11

alex prezmon


2 Answers

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.

like image 90
genespos Avatar answered Dec 22 '22 22:12

genespos


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
like image 44
Magnus Avatar answered Dec 22 '22 21:12

Magnus