How to use Object.GetType() method to gets the type of the current instance in VB.NET

4 Answers

0 votes
Module Module1

    Sub Main()

        Dim n As Integer = 8
        Dim l As Long = 673

        Console.WriteLine("n type: {0}", n.GetType())
        Console.WriteLine("l type: {0}", l.GetType())

    End Sub

End Module

' run:
' 
' n type: System.Int32
' l type : System.Int64

 



answered Apr 25, 2016 by avibootz
0 votes
Module Module1

    Sub Main()

        Dim n As Integer = 8
        Dim l As Long = 673

        Console.WriteLine("is n and l the same type: {0}",
                           Object.ReferenceEquals(n.GetType(), l.GetType()))

    End Sub

End Module

' run:
' 
' is n and l the same type: False

 



answered Apr 25, 2016 by avibootz
0 votes
Module Module1

    Sub Main()

        Dim o_types() As Object = {87, CLng(9835), CByte(3), CSByte(-8),
                                   3.14, CSng(5.87), "string"}

        For Each value In o_types
            Dim t As Type = value.GetType()

            If t.Equals(GetType(Byte)) Then
                Console.WriteLine("{0, 4}: is 8-bit unsigned byte.", value)
            ElseIf t.Equals(GetType(SByte)) Then
                Console.WriteLine("{0, 4}: is 8-bit signed byte.", value)
            ElseIf t.Equals(GetType(Integer)) Then
                Console.WriteLine("{0, 4}: is a 32-bit integer (int).", value)
            ElseIf t.Equals(GetType(Long)) Then
                Console.WriteLine("{0, 4}: is a 64-bit integer (long).", value)
            ElseIf t.Equals(GetType(Single)) Then
                Console.WriteLine("{0, 4}: is a single-precision floating-point (single).", value)
            ElseIf (t.Equals(GetType(Double))) Then
                Console.WriteLine("{0, 4}: is a double-precision floating-point (double).", value)
            ElseIf (t.Equals(GetType(String))) Then
                Console.WriteLine("'{0, 4}': is Unicode characters (string).", value)
            End If
        Next

    End Sub

End Module

' run:
' 
'   87: is a 32-bit integer (int).
' 9835: Is a 64-bit integer (long).
'    3: Is 8-bit unsigned byte.
'   -8: Is 8-bit signed byte.
' 3.14: Is a double-precision floating-point (double).
' 5.87: Is a single-precision floating-point (single).
' 'string': is Unicode characters (string).
'

 



answered Apr 25, 2016 by avibootz
0 votes
Module Module1

    Public Class BaseClass
    End Class

    Public Class DerivedClass : Inherits BaseClass
    End Class


    Sub Main()

        Dim base As New BaseClass()
        Dim derived As New DerivedClass()
        Dim o As Object = derived
        Dim bc As BaseClass = derived

        Console.WriteLine("base.GetType: {0}{1}", base.GetType(), vbNewLine)
        Console.WriteLine("derived.GetType: {0}{1}", derived.GetType(), vbNewLine)
        Console.WriteLine("Dim o As Object = derived; o.GetType: {0}{1}", o.GetType(), vbNewLine)
        Console.WriteLine("Dim bc As BaseClass = derived; bc.Type: {0}{1}", bc.GetType(), vbNewLine)

    End Sub

End Module

' run:
' 
' base.GetType: ConsoleApplication_Visual_Basic.Module1+BaseClass

' derived.GetType : ConsoleApplication_Visual_Basic.Module1+DerivedClass

' Dim o As Object = derived; o.GetType: ConsoleApplication_Visual_Basic.Module1+De
' rivedClass

' Dim bc As BaseClass = derived; bc.Type: ConsoleApplication_Visual_Basic.Module1+
' DerivedClass
'

 



answered Apr 27, 2016 by avibootz
...