23

Is there a way to get the instance's class name with VB.NET?

afe
  • 241
  • 1
  • 2
  • 3

4 Answers4

22

Try the following

Dim name = Me.GetType().Name

Or for any instance

Dim name = theObject.GetType().Name
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
22
Dim type As Type = yourObject.GetType()
Dim typeName As String = type.FullName

Full name will get you the fully qualified name of the Type, including the namespace of the Type.

See MSDN for more information on what is available with Type.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
1

This could be better when you are using asp.net website class not object.

Dim ClassName as string = Me.GetType().BaseType.FullName

OR

when you are using desktop app.

Dim ClassName as string = Me.GetType().Name
Rajan
  • 303
  • 1
  • 12
  • 1
    Me.GetType().BaseType.FullName doesn't get me the class name (for a class I coded). It simply returns "System.Object". But me.GetType().Name does return the name of my class. – Dan H. Jun 25 '18 at 14:45
1

Using Reflection, you could do the following...

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()

I use the following when logging trace info. This gives you the Class name, with all Namespaces and current Method name.

$"{System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()}.{MethodBase.GetCurrentMethod().Name}"
Chase
  • 162
  • 1
  • 10