Home All Groups Group Topic Archive Search About

Class name for current instance

Author
15 May 2006 2:32 PM
news.microsoft.com
How do I get the class name for a current instance. For example, if I want
to know the Class Name for the current form, how do I get this
programatically.

Thanks

Author
15 May 2006 2:54 PM
Stanimir Stoyanov
Hi,

You can use System.Reflection.MethodBase's [1] shared GetCurrentMethod
method to obtain information regarding the currently executed method. The
DeclaringType [2] property returns the type where the method is defined.

Sample code:

Imports System
Imports System.Reflection

Class TestClass
    Shared Sub Main(ByVal args() as String)
        Dim t as Type = MethodBase.GetCurrentMethod().DeclaringType
        If Not (t is Nothing) Then
            Console.WriteLine(t.FullName) ' Will output 'TestClass'
        End If
    End Sub
End Class

[1]:
http://msdn2.microsoft.com/en-US/library/system.reflection.methodbase(VS.80).aspx
[2]:
http://msdn2.microsoft.com/en-us/library/system.reflection.memberinfo.declaringtype.aspx
--
Stanimir Stoyanov
www.stoyanoff.info


Show quoteHide quote
"news.microsoft.com" wrote:

> How do I get the class name for a current instance. For example, if I want
> to know the Class Name for the current form, how do I get this
> programatically.
>
> Thanks
>
>
>
Author
15 May 2006 3:16 PM
Marina Levit [MVP]
That will get the name of type that declares the method - which could be an
ancestor. That's not the same as the name of the type of the current
instance.

Also, FullName gives the fully qualified name - including namespace.

To get just the name of the current instance:

Dim typeName as String = Me.GetType().Name

Show quoteHide quote
"Stanimir Stoyanov" <admin{at}stoyanoff{dot}info> wrote in message
news:8A105E57-68F3-4D81-98CB-8671F8A1D993@microsoft.com...
> Hi,
>
> You can use System.Reflection.MethodBase's [1] shared GetCurrentMethod
> method to obtain information regarding the currently executed method. The
> DeclaringType [2] property returns the type where the method is defined.
>
> Sample code:
>
> Imports System
> Imports System.Reflection
>
> Class TestClass
> Shared Sub Main(ByVal args() as String)
> Dim t as Type = MethodBase.GetCurrentMethod().DeclaringType
> If Not (t is Nothing) Then
> Console.WriteLine(t.FullName) ' Will output 'TestClass'
> End If
> End Sub
> End Class
>
> [1]:
> http://msdn2.microsoft.com/en-US/library/system.reflection.methodbase(VS.80).aspx
> [2]:
> http://msdn2.microsoft.com/en-us/library/system.reflection.memberinfo.declaringtype.aspx
> --
> Stanimir Stoyanov
> www.stoyanoff.info
>
>
> "news.microsoft.com" wrote:
>
>> How do I get the class name for a current instance. For example, if I
>> want
>> to know the Class Name for the current form, how do I get this
>> programatically.
>>
>> Thanks
>>
>>
>>
Author
15 May 2006 4:26 PM
Herfried K. Wagner [MVP]
Marina,

"Marina Levit [MVP]" <someone@nospam.com> schrieb:
> That will get the name of type that declares the method - which could be
> an ancestor. That's not the same as the name of the type of the current
> instance.

That's true.  However, it makes sense when dealing with shared members as
'Me.GetType()' doesn't work inside these methods :-).

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
15 May 2006 6:19 PM
Marina Levit [MVP]
Right, the question did specifically ask how to get the name of the 'current
instance', so that implied there was an instance in question. So I just
responded to that.

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:ehPYDxDeGHA.3484@TK2MSFTNGP04.phx.gbl...
> Marina,
>
> "Marina Levit [MVP]" <someone@nospam.com> schrieb:
>> That will get the name of type that declares the method - which could be
>> an ancestor. That's not the same as the name of the type of the current
>> instance.
>
> That's true.  However, it makes sense when dealing with shared members as
> 'Me.GetType()' doesn't work inside these methods :-).
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
16 May 2006 9:44 AM
news.microsoft.com
Thank you all very much for this information, it has helped me progress with
what I was doing.

It's nice to see that people can be so helpful.




Show quoteHide quote
"Marina Levit [MVP]" <someone@nospam.com> wrote in message
news:%238$5awEeGHA.4720@TK2MSFTNGP03.phx.gbl...
> Right, the question did specifically ask how to get the name of the
> 'current instance', so that implied there was an instance in question. So
> I just responded to that.
>
> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
> news:ehPYDxDeGHA.3484@TK2MSFTNGP04.phx.gbl...
>> Marina,
>>
>> "Marina Levit [MVP]" <someone@nospam.com> schrieb:
>>> That will get the name of type that declares the method - which could be
>>> an ancestor. That's not the same as the name of the type of the current
>>> instance.
>>
>> That's true.  However, it makes sense when dealing with shared members as
>> 'Me.GetType()' doesn't work inside these methods :-).
>>
>> --
>> M S   Herfried K. Wagner
>> M V P  <URL:http://dotnet.mvps.org/>
>> V B   <URL:http://classicvb.org/petition/>
>
>
Author
16 May 2006 1:07 PM
Herfried K. Wagner [MVP]
"Marina Levit [MVP]" <someone@nospam.com> schrieb:
> Right, the question did specifically ask how to get the name of the
> 'current instance', so that implied there was an instance in question. So
> I just responded to that.

There is nothing wrong with your reply!  I simply wanted to make the OP
aware that there are certain cases where the method described by Stanimir
makes sense.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
15 May 2006 5:34 PM
Peter Macej
What about TypeName function?

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Author
16 May 2006 12:08 PM
news.microsoft.com
The suggested reflection method seems to do the trick nicely

Thanks


Show quoteHide quote
"Peter Macej" <pe***@vbdocman.com> wrote in message
news:udk8LXEeGHA.1856@TK2MSFTNGP03.phx.gbl...
> What about TypeName function?
>
> --
> Peter Macej
> Helixoft - http://www.vbdocman.com
> VBdocman - Automatic generator of technical documentation for VB, VB .NET
> and ASP .NET code