开发者

Returning multiple values from a function in an parcial class in VB

Hi Please can someone show me how to return multiple values from a function? I have my function in a seperate file within App_Code and here it is:

Public Function GetQuoteStatus(ByVal QuoteID As String) As String 

D开发者_如何学运维im quoteStatus As String

Dim con As New SqlConnection  
Dim cmd As New SqlCommand  

Try  
    con.ConnectionString = ConfigurationManager.AppSettings("quotingSystemConnectionString")  
    con.Open()  
    cmd.Connection = con  
    cmd.CommandText = "SELECT Status FROM Quote WHERE QuoteID =" & QuoteID & ";"  
    Dim lrd As SqlDataReader = cmd.ExecuteReader()  

    While lrd.Read()  
        quoteStatus = lrd("Status")  
    End While  

Catch ex As Exception  

Finally  
    con.Close()  
End Try  

Return quoteStatus

End Function

To get the returned quoteStatus from within another vb file in my site I would normally use:

Dim statusHelper As New quoteStatusHelper()
Dim quoteStatus As String = statusHelper.GetQuoteStatus("QuoteID")

However this is all good and well for one returned value but what If I wanted to return multiple values... How do I access them?

Many thanks!


You could use

Public Function GetQuoteStatus(ByVal QuoteID As String) As String()

This let you return a string array, so there is no limit to data you can have.
The cons is you have to parse the array.

You could even use

Public Function GetQuoteStatus(ByVal QuoteID As String, 
    Byref Second As String) As String

This let you return a string as result and set the value of another var (Second); you can use more than one Byref variables to return multiple values...
The cons is that you have to statically declare your function and modifiy all previous calls. In this case the use of refactoring is reccomended!

EDITED:
You could even return a class for example.
Declare a class that fits your needs (with all the fields, getters, setters and constructors) and inside your function you could create an instance of that class, fill every field and return this class.
Easy to implement, easy to use.

EDITED AGAIN:

Public Class MyClass
    Public Property Val1 As String
    Public Property Val2 As String
    Public Property Val3 As String

    Public Sub New(ByVal newVal1 As String, ByVal newVal2 As String, ByVal newVal3 As String)
        Val1 = newVal1
        Val2 = newVal2
        Val3 = newVal3
    End Sub
End Class

Public Function GetInfo() As MyClass
    Dim mc As New MyClass("test1", "test2", "test3")
    Return mc
End Function


You may return multiple values using ByRef and arrays (see @Marco post). You may also return/pass an object for your purpose.

For instance,

Public Class Info
    Public Property No As Integer
    Public Property Name As String
End Class
....
 Public Function GetInfo() As Info
        Dim inf As New Info
        inf.No = 10
        inf.Name = "A"
        Return inf
 End Function
....


I believe that the "correct/best" way to deal with a single function that needs to return a couple of discrete (typically primitive) types is a Tuple(Of T1, T2, T3, T4, T5, T6, T7, TRest).

Something like this

Public Function MyExampleMethod() As Tuple(Of String, Integer, Guid)
    Return New Tuple(Of String, Integer, Guid)("Value1", 2, Guid.NewGuid)
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜