473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Class name for current instance

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
8 8817
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gry | last post by:
I often find myself wanting an instance attribute that can take on only a few fixed symbolic values. (This is less functionality than an enum, since there are no *numbers* associated with the...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
by: Don Miller | last post by:
I am using a Session variable to hold a class object between ASP.NET pages (in VB). In my constructor I check to see if the Session variable exists, and if it doesn't, I create one and populate it...
0
by: Fredrik Lundh | last post by:
Robert Rawlins wrote: Name or namespace? You can access the class name from an instance via the __class__ attribute: .... def __repr__(self): .... return "<%s instance at %x>"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.