Vb.net same function return more than one data types

rflorencio
rflorencio used Ask the Experts™
on
I have the following function:

   Public Shared Function GetField(ByVal xSqlstring As String) As String
        Dim conn As SqlConnection = New SqlConnection(ConnectionString)
        Dim Cmd As New SqlCommand(xSqlstring, conn)
        conn.Open()
        Dim xField As String = Cmd.ExecuteScalar()
        conn.Close()
        Return xField
    End Function

How can return different data types like integer, date ....
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Public Shared Function GetField(ByVal xSqlstring As String) As Object        Dim conn As SqlConnection = New SqlConnection(ConnectionString)        Dim Cmd As New SqlCommand(xSqlstring, conn)        conn.Open()        Dim xField As Object= Cmd.ExecuteScalar()        conn.Close()        Return xField    End FunctionConvert the object into what ever datatype you want in the calling function

Commented:
Return the required result in the function or if your returning a object convert into required data type like integer or dattime and use
Generally, you would have separate functions, similar to the one you show, but specialized for each possible Return Type.  You would need to know, from your code, which Return Type was to be expected, so that the correct function could be called.
 
For Example
   Public Shared Function GetIntegerField(ByVal xSqlstring As String) As String
        Dim conn As SqlConnection = New SqlConnection(ConnectionString)
        Dim Cmd As New SqlCommand(xSqlstring, conn)
        conn.Open()
        Dim xField As Integer = Cmd.ExecuteScalar()
        conn.Close()
        Return xField
    End Function
   Public Shared Function GetDateField(ByVal xSqlstring As String) As String
        Dim conn As SqlConnection = New SqlConnection(ConnectionString)
        Dim Cmd As New SqlCommand(xSqlstring, conn)
        conn.Open()
        Dim xField As Date = Cmd.ExecuteScalar()
        conn.Close()
        Return xField
    End Function
 
There is no way that a single Function can be able to return a value other that the type that is specified in the Function declaration.
AW


 
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Commented:
Another idea. I probably would not use it myself.

Return a HashTable with a single element. Hashtables can hold all kind of types and they don't need to be specified.


Public Shared Function GetField(ByVal xSqlstring As String) As HashTable
    Dim conn As SqlConnection = New SqlConnection(ConnectionString)
    Dim Cmd As New SqlCommand(xSqlstring, conn)
    conn.Open()
    Dim xField As New HashTable
    xField("result") = Cmd.ExecuteScalar()
    conn.Close()
    Return xField
End Function

Open in new window

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
I have a function like yours. I have been able to overload it by adding a second parameter that is my default value in the same datatype as my return value

Author

Commented:
but if function returns ever a string (in this case). I can pass a parameter with data type, but when return is string. I would have a function with the same name and to return the correct data type.
Senior .Net Consultant
Top Expert 2016
Commented:
it would be something like this:
 

   Public Shared Function GetField(ByVal xSqlstring As String, byval pDefault as string) As String
        Dim conn As SqlConnection = New SqlConnection(ConnectionString)
        Dim Cmd As New SqlCommand(xSqlstring, conn)
        conn.Open()
        Dim xField As String = Cmd.ExecuteScalar()
        conn.Close()
if string.isnullorempty(xField) then
    return pDefault
else
        Return xField
endif
    End Function

   Public Shared Function GetField(ByVal xSqlstring As String, byval pDefault as integer) As integer
        Dim conn As SqlConnection = New SqlConnection(ConnectionString)
        Dim Cmd As New SqlCommand(xSqlstring, conn)
        conn.Open()
        Dim xField As String = Cmd.ExecuteScalar()
        conn.Close()
if string.isnullorempty(xField) then
    return pDefault
else
        Return cint( xField)
endif
    End Function

Open in new window

Author

Commented:
good thinking

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial