Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Multiple variable-types in a function?

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
I need to pass a short value and then a value of any stock type to a function and am having difficulty figuring out how to do so. I could overload the function a billion times, but that's wasteful. I have read that you can pass the "Object" class in place of anything, but how do I then determine what was passed? For example, I might call the function with a short and a string one time, but a short and an integer the next time. How is this done in VB.NET?

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • zibadianzibadian Member Posts: 6,349
    : I need to pass a short value and then a value of any stock type to a
    : function and am having difficulty figuring out how to do so. I
    : could overload the function a billion times, but that's wasteful. I
    : have read that you can pass the "Object" class in place of anything,
    : but how do I then determine what was passed? For example, I might
    : call the function with a short and a string one time, but a short
    : and an integer the next time. How is this done in VB.NET?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    You can use the GetType() method for this: http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    : You can use the GetType() method for this:
    : http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx

    Thanks for the info, I'll read up on it in a while. I hate to post such simple problems, but the new MSDN sucks. I'll type in "vb .net object class type" and I get a billion links to useless forum posts, whereas I used to get meaningful results from their documentation library, like what you've posted a link to. I don't know what it is with search engines, but they seem to be getting MUCH less intelligent here lately, aside from Google.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    : You can use the GetType() method for this:
    : http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx

    After fooling with this for an hour and a half today, I can't figure out how to get the data out of the object. I figured out how to get the type of variable, but not the data. My modification method for the class is below, to give you a better idea of what's going on.
    [code]
    'Method to modify this configuration
    Public Function Modify(ByRef keyShort As Short, ByVal valueObject As Object) As Boolean
    'Verify that this object is initialized
    If Not Me.initializedBool Then Return False

    'Figure out if a sub-object is being modified
    If keyShort > 5 And keyShort < 12 Then
    Return Me.systemObject.Modify(keyShort, valueObject)
    ElseIf keyShort > 11 And keyShort < 16 Then
    Return Me.soundObject.Modify(keyShort, valueObject)
    ElseIf keyShort > 15 And keyShort < 24 Then
    Return Me.videoObject.Modify(keyShort, valueObject)
    Else
    Return False
    End If

    'Figure out what is being updated and update it
    Select Case keyShort
    Case MODIFY_ARCHITECTURE
    If valueObject.GetType().ToString() <> "String" Then Return False
    'Now how do I get the string value out??
    Exit Select

    Case MODIFY_NAME
    Exit Select

    Case MODIFY_EXE
    Exit Select

    Case MODIFY_NET
    Exit Select

    Case MODIFY_CFG
    Exit Select

    Case MODIFY_NOTES
    Exit Select

    Case Else
    Return False
    End Select

    Return True
    End Function
    [/code]
    Note my first case in the select block. That is where I am hitting a wall. I can find methods in the object class to compare it to other object classes, but assuming I had passed a string value, how would I get it out of the object?

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • zibadianzibadian Member Posts: 6,349
    : : You can use the GetType() method for this:
    : : http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx
    :
    : After fooling with this for an hour and a half today, I can't figure
    : out how to get the data out of the object. I figured out how to get
    : the type of variable, but not the data. My modification method for
    : the class is below, to give you a better idea of what's going on.
    : [code]:
    : 'Method to modify this configuration
    : Public Function Modify(ByRef keyShort As Short, ByVal valueObject As Object) As Boolean
    : 'Verify that this object is initialized
    : If Not Me.initializedBool Then Return False
    :
    : 'Figure out if a sub-object is being modified
    : If keyShort > 5 And keyShort < 12 Then
    : Return Me.systemObject.Modify(keyShort, valueObject)
    : ElseIf keyShort > 11 And keyShort < 16 Then
    : Return Me.soundObject.Modify(keyShort, valueObject)
    : ElseIf keyShort > 15 And keyShort < 24 Then
    : Return Me.videoObject.Modify(keyShort, valueObject)
    : Else
    : Return False
    : End If
    :
    : 'Figure out what is being updated and update it
    : Select Case keyShort
    : Case MODIFY_ARCHITECTURE
    : If valueObject.GetType().ToString() <> "String" Then Return False
    : 'Now how do I get the string value out??
    : Exit Select
    :
    : Case MODIFY_NAME
    : Exit Select
    :
    : Case MODIFY_EXE
    : Exit Select
    :
    : Case MODIFY_NET
    : Exit Select
    :
    : Case MODIFY_CFG
    : Exit Select
    :
    : Case MODIFY_NOTES
    : Exit Select
    :
    : Case Else
    : Return False
    : End Select
    :
    : Return True
    : End Function
    : [/code]:
    : Note my first case in the select block. That is where I am hitting
    : a wall. I can find methods in the object class to compare it to
    : other object classes, but assuming I had passed a string value, how
    : would I get it out of the object?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    Type-cast the object as a String class. This can safely be done, since you already know that the object is a String (or descendant of that class).
    Something like this:
    [code]
    Dim valueObject As String = DirectCast(valueObject, String)
    [/code]
    Source: http://www.fmsinc.com/free/NewTips/NET/NETtip6.asp
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    : Type-cast the object as a String class. This can safely be done,
    : since you already know that the object is a String (or descendant of
    : that class).
    : Something like this:
    : [code]:
    : Dim valueObject As String = DirectCast(valueObject, String)
    : [/code]:
    : Source: http://www.fmsinc.com/free/NewTips/NET/NETtip6.asp
    Thanks, that seems to have been the missing-link. Now I just have a little more coding to do for the forms and the application will be a working beta.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Sign In or Register to comment.