By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Class name for current instance

P: n/a
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
May 15 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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/lib...se(VS.80).aspx
[2]:
http://msdn2.microsoft.com/en-us/lib...aringtype.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

May 15 '06 #2

P: n/a
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

"Stanimir Stoyanov" <admin{at}stoyanoff{dot}info> wrote in message
news:8A**********************************@microsof t.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/lib...se(VS.80).aspx
[2]:
http://msdn2.microsoft.com/en-us/lib...aringtype.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

May 15 '06 #3

P: n/a
Marina,

"Marina Levit [MVP]" <so*****@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/>

May 15 '06 #4

P: n/a
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
May 15 '06 #5

P: n/a
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]" <hi***************@gmx.at> wrote in message
news:eh**************@TK2MSFTNGP04.phx.gbl...
Marina,

"Marina Levit [MVP]" <so*****@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/>

May 15 '06 #6

P: n/a
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.


"Marina Levit [MVP]" <so*****@nospam.com> wrote in message
news:%2****************@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]" <hi***************@gmx.at> wrote in message
news:eh**************@TK2MSFTNGP04.phx.gbl...
Marina,

"Marina Levit [MVP]" <so*****@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/>


May 16 '06 #7

P: n/a
The suggested reflection method seems to do the trick nicely

Thanks
"Peter Macej" <pe***@vbdocman.com> wrote in message
news:ud**************@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

May 16 '06 #8

P: n/a
"Marina Levit [MVP]" <so*****@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/>

May 16 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.