rp
asked on
Vb.net same function return more than one data types
I have the following function:
Public Shared Function GetField(ByVal xSqlstring As String) As String
Dim conn As SqlConnection = New SqlConnection(ConnectionSt ring)
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 ....
Public Shared Function GetField(ByVal xSqlstring As String) As String
Dim conn As SqlConnection = New SqlConnection(ConnectionSt
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 ....
Public Shared Function GetField(ByVal xSqlstring As String) As Object Dim conn As SqlConnection = New SqlConnection(ConnectionSt ring) 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
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(ConnectionSt ring)
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(ConnectionSt ring)
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
For Example
Public Shared Function GetIntegerField(ByVal xSqlstring As String) As String
Dim conn As SqlConnection = New SqlConnection(ConnectionSt
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(ConnectionSt
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
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.
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
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
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
good thinking