473,851 Members | 2,037 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.MyFunc tion"
End Sub
End Class
- Don
Nov 21 '05 #1
2 5859

"Don" <un*****@oblivi on.com> wrote in message
news:F7PQd.4092 02$Xk.355732@pd 7tw3no...
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.MyFunc tion"
End Sub
End Class
- Don

Have a look in the System.Diagnost ics 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.Reflecti on.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.no spam.co.uk> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..

"Don" <un*****@oblivi on.com> wrote in message
news:F7PQd.4092 02$Xk.355732@pd 7tw3no...
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.Diagnost ics 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.Reflecti on.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.Reflecti on.MethodBase

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

Console.WriteLi ne(method.Refle ctedType.Namesp ace & "." &
method.Reflecte dType.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
2327
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 `eck<>' for `void blah<int>::eck(int)' does not match any template declaration ttest.cpp:23: syntax error before `{' token ttest.cpp:25: syntax error before `<<' token Note that 'gak' (a template function in a template class) works, as
14
2044
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 I need to 'disable' the code in M_B on C ? The correct way to do that seems to be with a M_C metaclass, subclass of M_B, implementing but not calling parent class methods, or calling 'type' methods.
0
1868
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, during the processing time. The first question is: Is it possible to have an overlap window? (I create my windows by the "CreateWindowEx" command) By which command
2
9788
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 towards this approach such as how would it works in multi-thread. Thanks Tony
4
1856
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 access a method of Class2 (say objC2.func1(); Now in function 'func1()' of Class2 I wanna get the Caller class name ('Class1' Plz don say me to pass the name of class as an arg in 'func()'; i need som other way ... this is coz Class2 is a central...
0
2207
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 of the data that it provides. However, although I can get the initialisations to work, I cannot manage to get responses that require data to pass to the dll. (The latest API details are at : ftp://ftp.t11.org/t11/pub/sm/hba/05-056v0.pdf) The...
8
1753
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 f1(self): print 'In A.f1, calling func' self.func() def func(self):
4
2259
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 do is call a generically named public sub in the top-most sub form in the panel. I have the name of the top-most form as a string (eg. g_TopForm = "frmCustomers") and I can reference the form with:
2
1709
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 someone could help me out. Now first of all I want to describe the setting in a simpler (I'm using Visual Studio 2005): Form: One comboBox (dropdown list) and 2 buttons. Classes: Class1 with a function do() which plainly returns a string.
0
11019
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10728
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9506
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7906
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5933
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4549
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 we have to send another system
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3179
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.