473,396 Members | 1,734 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,396 software developers and data experts.

Returning Function or Sub Name

Like every developer, I've got tons of error trapping code that hard code
the name of the function or sub name so I can error log it. It seems from
other discussions that the only way to get the function or subprocedure name
is to either hard code it or to use a Constant to store it and reference the
Constant in each Catch statement. There are no properties that return the
function/sub name. Has anyone found a way thru reflection to get the name of
the currently running fuction/subprocedure name?

Thank you so much for your help.
Feb 24 '07 #1
4 1328
John Simpson wrote:
Has anyone
found a way thru reflection to get the name of the currently running
fuction/subprocedure name?
System.Reflection.MethodBase.GetCurrentMethod.Name

HTH,

--

(O)enone
Feb 24 '07 #2
That's got it. Thank you again for your help.

"(O)enone" wrote:
John Simpson wrote:
Has anyone
found a way thru reflection to get the name of the currently running
fuction/subprocedure name?

System.Reflection.MethodBase.GetCurrentMethod.Name

HTH,

--

(O)enone
Feb 24 '07 #3
John Simpson wrote:
Like every developer, I've got tons of error trapping code that hard code
the name of the function or sub name so I can error log it. It seems from
other discussions that the only way to get the function or subprocedure name
is to either hard code it or to use a Constant to store it and reference the
Constant in each Catch statement. There are no properties that return the
function/sub name. Has anyone found a way thru reflection to get the name of
the currently running fuction/subprocedure name?

Thank you so much for your help.
The exception object contains a stack trace that contains the name of
the method, and also the methods that was called to get there.

--
Göran Andersson
_____
http://www.guffa.com
Feb 24 '07 #4
John,
As Göran suggests, check out the Exception.StackTrace property.

http://msdn2.microsoft.com/en-us/lib...tacktrace.aspx
Like every developer, I've got tons of error trapping code that hard code
the name of the function or sub name so I can error log it.
Unlike VB6 & other COM based languages. .NET itself has all the error
trapping code to track the name of functions, subs, properties allowing you
to log rich Exception objects that include stack traces & other contextual
information; such as the Exception.Data collection:

http://msdn2.microsoft.com/en-us/lib...tion.data.aspx

Has anyone found a way thru reflection to get the name of
the currently running fuction/subprocedure name?
Although you can use GetCurrentMethod.Name as (O)enone suggests. Simply
throwing & catch Exception objects avoids needing to know the name of
functions, subs, and properties. Further it avoids needing to code "tons of
error trapping code" to log it. I normally include "pounds, maybe ounces, of
error trapping code" at higher levels that record the entire Exception
object. At lower level, I may include a Try/Catch simply so the Catch can
include further contextual information (such as parameter values) in either
the Exception.Data property or custom properties on custom Exception classes
in the exception that is thrown to the higher levels.

Mostly my lower levels use Try/Finally or the new Using statement ensure
resources (IDisposable objects such as SQL Connection objects & System
Drawing/GDI+ objects) are property disposed of.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"John Simpson" <Jo*********@discussions.microsoft.comwrote in message
news:D7**********************************@microsof t.com...
Like every developer, I've got tons of error trapping code that hard code
the name of the function or sub name so I can error log it. It seems from
other discussions that the only way to get the function or subprocedure
name
is to either hard code it or to use a Constant to store it and reference
the
Constant in each Catch statement. There are no properties that return the
function/sub name. Has anyone found a way thru reflection to get the name
of
the currently running fuction/subprocedure name?

Thank you so much for your help.
Feb 25 '07 #5

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

Similar topics

2
by: Mountain Man | last post by:
Hi, I'm having trouble with the foreach function. I'm using it twice inside a user defined function with two different arrays, and in the second instance it's returning the value of the first...
7
by: Dr John Stockton | last post by:
What are the best ways of returning multiple results from a subroutine ? I've been using ... return } which is inelegant. I'm used to Pascal's procedure X(const A, B : integer; var C, D :...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
1
by: Guha | last post by:
I have a problem with returning a 2D array using a function which is called in main(). The piece of the code is given below. This is a test code only. #include"stdio.h" #include"alloc.h" ...
3
by: darrel | last post by:
This might be a really dumb question, but when should/shouldn't one use the exit function command? If I have this: function() if something then do this return that else
3
by: Sean Tynan | last post by:
I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution. e.g. say I have a method (business...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
2
by: Ampodayas | last post by:
I've seen alot of confusing explanations for how to return a function pointer without a typedef, so here it is in plain english: (Return of func pointer being returned) ((class name)::(function...
7
Haitashi
by: Haitashi | last post by:
I have a form that calls a function using the onClick event in the submit button. I would like this function to return false if the username is taken. The the original function, which I've tested,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.