473,508 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Possible to get name of calling class and function?

Don
This may seem like an odd question, but is it possible to get the name of
the class & function that is calling a function of another class?

e.g.

Public Class CallerName
Public Shared Function Test() As String
Return <the name of my caller> ' <--- is there a way to do this?
End Sub
End Class

Public Class MyClass
Public Shared Sub MyFunction
Console.Write CallerName.Test '<-- spits out "MyClass.MyFunction"
End Sub
End Class
- Don
Nov 21 '05 #1
2 5839

"Don" <un*****@oblivion.com> wrote in message
news:F7PQd.409202$Xk.355732@pd7tw3no...
This may seem like an odd question, but is it possible to get the name of
the class & function that is calling a function of another class?

e.g.

Public Class CallerName
Public Shared Function Test() As String
Return <the name of my caller> ' <--- is there a way to do this?
End Sub
End Class

Public Class MyClass
Public Shared Sub MyFunction
Console.Write CallerName.Test '<-- spits out "MyClass.MyFunction"
End Sub
End Class
- Don

Have a look in the System.Diagnostics namespace at the classes StackTrace
and StackFrame. The StackTrace will allow you to obtain a reference to the
StackFrame above the current one (i.e. the calling method). You can then
obtain a System.Reflection.MethodBase which will contain all the information
you need. Note, this will probably only work as expected in Debug builds
(as the naming information is not available in Release builds).

Hope this helps,

Nick Hall
Nov 21 '05 #2
Don

"Nick Hall" <ni***@aslan.nospam.co.uk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Don" <un*****@oblivion.com> wrote in message
news:F7PQd.409202$Xk.355732@pd7tw3no...
This may seem like an odd question, but is it possible to get the name of the class & function that is calling a function of another class?
Have a look in the System.Diagnostics namespace at the classes StackTrace
and StackFrame. The StackTrace will allow you to obtain a reference to

the StackFrame above the current one (i.e. the calling method). You can then
obtain a System.Reflection.MethodBase which will contain all the information you need. Note, this will probably only work as expected in Debug builds
(as the naming information is not available in Release builds).


Thanks for the tips. Here's what I ended up doing (more or less):
Dim strace As New StackTrace
Dim frame As New StackFrame
Dim method As System.Reflection.MethodBase

frame = strace.GetFrame(1) ' Gets the stack frame for the method that called
this one
method = frame.GetMethod ' Grab method info

Console.WriteLine(method.ReflectedType.Namespace & "." &
method.ReflectedType.Name & "." & method.Name)
- Don
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2306
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id...
14
2011
by: Pedro Werneck | last post by:
Hi I have a class A, with metaclass M_A, and class B, subclass of A, with metaclass M_B, subclass of M_A. A class C, subclass of B must have M_B or a subclass of it as metaclass, but what if...
0
1840
by: dag | last post by:
Hi! I would like to do an overlap window, over my main window (of my application), with a Progress Bar. Exactly when I push a button of my application I want show a window, with a Progress bar,...
2
9750
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
4
1833
by: Hmnt | last post by:
Hi Hope ur smart enough to get what i wanna ask ... (from the sub line Brief Explaination I've 2 classas Class1 and Class Now i've an instance of Class2 ( Class2 objC2 = new Class2(); Next i...
0
2192
by: Vizzybit | last post by:
I am attempting to utilise the HBAAPI.dll that can be found at http://hbaapi.sourceforge.net/ (which is also used in hbaverify at http://hbaverify.sourceforge.net/) as I need to get a small subset...
8
1734
by: Jeffrey Barish | last post by:
I believe that the answer to my question is no, but I want to be sure that I understand this issue correctly: Suppose that there are two classes defined as follows: class A(object): def...
4
2223
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to...
2
1690
by: Chicken15 | last post by:
Hi Group. First of all I'm sorry for asking (maybe) such easy questions. But I'm quite stuck now and couldn't come up with a solution by using my C# book or googling. So it would be nice if...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.