从函数 vb.net 返回 2 个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33720474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:33:58  来源:igfitidea点击:

Return 2 values from function vb.net

vb.net

提问by alex prezmon

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.

我想要的是使用函数从数据库中返回 2 个值,然后将这些值存储在变量中,以便我可以使用它们。这是我的代码。

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:\Visual00Phrases00 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

我不知道该怎么做。该函数仅返回“a”的值谢谢

回答by genespos

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.

这就是我的想法。任何人都可以有不同的想法。

回答by Magnus

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

从 VB 15 开始,您可以使用 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

回答by SupermanKelly

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

我是 .net 的新手,但“ByRef”不是最简单的方法吗?

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