none
Getting parameter's type RRS feed

  • Question

  • Hi

    My SUB has an Object parameter. How can I within the SUB get the parameter's type and possibly cast it to the type?

    Thanks

    Regards

    Thursday, April 18, 2019 5:38 PM

Answers

  • Hi

    Usually, you would know what is calling the Sub. For example, a Button click event handler Sub would have:

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    this is for a single Button, but, you may have several Buttons calling the same Sub (such as):
        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    

    where 3 Buttons are set to use the same click event handler.

    Now, we know that all callers are of type Button, so we can do this:

        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click,
            Button2.Click,
            Button3.Click
            Dim butt As Button = DirectCast(sender, Button)
            Select Case butt.Name
                Case "Button1"
                    '...... do something
                Case "Button2"
                    '...... do something
                Case "Button3"
                    '...... do something
            End Select
    So you see that there isn't a stock answer to your question. You the programmer need to know what would be calling your Sub and what type it is.


    Regards Les, Livingston, Scotland

    Thursday, April 18, 2019 6:20 PM
  • This illustrates getting the type.

        Private Function Dummy(something As Object) As Type
            Dim typ As Type
            If something IsNot Nothing Then
                typ = something.GetType
            End If
            Return typ
        End Function
    

    Use

            Dim t As Type
            t = Dummy(sender)
            Dim i As Integer
            t = Dummy(i)
            Dim s As String = ""
            t = Dummy(s)
    
    Once you have  you could do this

            Select Case True
                Case t Is Nothing
                    Stop
                Case t = GetType(String)
                    Stop
                Case t = GetType(Int32)
                    Stop
            End Select
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Marked as answer by Y a h y a Thursday, April 18, 2019 9:37 PM
    Thursday, April 18, 2019 6:38 PM
  • Hi,
    you can use GetType to detect the type of object and then switch to execute specific code, like in this console demo:

    Module Module14
    
      Sub Main()
        Try
          Dim c As New Demo
          c.Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
    
      Friend Class Demo
    
        Friend Sub Execute()
          Console.WriteLine($"Integer: {F1(55)}")
          Console.WriteLine($"String: {F1("demo String")}")
          Console.WriteLine($"Single: {F1(55.5!)}")
          Console.WriteLine($"Class: {F1(Me)}")
        End Sub
    
        Private Function F1(par As Object) As String
          Select Case par.GetType
            Case GetType(Integer)
              Dim i As Integer = CType(par, Integer)
              Return i.ToString
            Case GetType(Double)
              Dim d As Single = CType(par, Single)
              Return d.ToString
            Case GetType(String)
              Return par.ToString
            Case Else
              Return par.ToString
          End Select
        End Function
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by Y a h y a Thursday, April 18, 2019 9:37 PM
    Thursday, April 18, 2019 6:51 PM

All replies

  • Hi

    Usually, you would know what is calling the Sub. For example, a Button click event handler Sub would have:

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    this is for a single Button, but, you may have several Buttons calling the same Sub (such as):
        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    

    where 3 Buttons are set to use the same click event handler.

    Now, we know that all callers are of type Button, so we can do this:

        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click,
            Button2.Click,
            Button3.Click
            Dim butt As Button = DirectCast(sender, Button)
            Select Case butt.Name
                Case "Button1"
                    '...... do something
                Case "Button2"
                    '...... do something
                Case "Button3"
                    '...... do something
            End Select
    So you see that there isn't a stock answer to your question. You the programmer need to know what would be calling your Sub and what type it is.


    Regards Les, Livingston, Scotland

    Thursday, April 18, 2019 6:20 PM
  • This illustrates getting the type.

        Private Function Dummy(something As Object) As Type
            Dim typ As Type
            If something IsNot Nothing Then
                typ = something.GetType
            End If
            Return typ
        End Function
    

    Use

            Dim t As Type
            t = Dummy(sender)
            Dim i As Integer
            t = Dummy(i)
            Dim s As String = ""
            t = Dummy(s)
    
    Once you have  you could do this

            Select Case True
                Case t Is Nothing
                    Stop
                Case t = GetType(String)
                    Stop
                Case t = GetType(Int32)
                    Stop
            End Select
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Marked as answer by Y a h y a Thursday, April 18, 2019 9:37 PM
    Thursday, April 18, 2019 6:38 PM
  • Hi,
    you can use GetType to detect the type of object and then switch to execute specific code, like in this console demo:

    Module Module14
    
      Sub Main()
        Try
          Dim c As New Demo
          c.Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
    
      Friend Class Demo
    
        Friend Sub Execute()
          Console.WriteLine($"Integer: {F1(55)}")
          Console.WriteLine($"String: {F1("demo String")}")
          Console.WriteLine($"Single: {F1(55.5!)}")
          Console.WriteLine($"Class: {F1(Me)}")
        End Sub
    
        Private Function F1(par As Object) As String
          Select Case par.GetType
            Case GetType(Integer)
              Dim i As Integer = CType(par, Integer)
              Return i.ToString
            Case GetType(Double)
              Dim d As Single = CType(par, Single)
              Return d.ToString
            Case GetType(String)
              Return par.ToString
            Case Else
              Return par.ToString
          End Select
        End Function
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by Y a h y a Thursday, April 18, 2019 9:37 PM
    Thursday, April 18, 2019 6:51 PM