Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic return data type

Status
Not open for further replies.

tutuster

Programmer
Feb 2, 2005
41
0
0
FI
I'm looking for a solution to create a function,that retrieves actual data (as String format), and returns the value converted to a specific data type.

For example, function gets a string "True", and it could be returned as Boolean (True), or whatever datatype (of course conversion from "true" to Double, for example, would cause a handled "CannotConvertException")

The idea is, that the calling method could spesify the data type as Optional Byval, if it is nothing (default), return type will always be String.

Is this possible to do? If it is, kick me with a hint.

Of course, I could pass the return type as string and use select case for conversion. But I would like to know it this is possible to solve some easier/more effective way.

Yours,
Des
 
this should give you a head start, Button3_click was a method for when a button was clicked, which would run the function that follows. You have to check types going in and out, but this is how I would approach it.


Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Obj As Object
        Obj = DemoReturn(GetType(String))

        If Obj.GetType Is GetType(String) Then
            MsgBox("String was returned")
        End If
        If Obj.GetType Is GetType(Boolean) Then
            MsgBox("boolean was returned")
        End If

    End Sub

    Private Function DemoReturn(ByVal type As Type) As Object
        If type Is GetType(String) Then
            Return "True as String"
        ElseIf type Is GetType(Boolean) Then
            Return True
        End If
    End Function

-Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top