473,405 Members | 2,171 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,405 software developers and data experts.

Dynamically Get Sub Name

Hi, Is there a way to dynamically get the name of the current Sub?
In my code below in the exception "catch" i write the name of the Sub to the
error log so that i will know where there error happened, but since i reuse
this code in other Subs i sometimes forget to change the name of the Sub. is
there a way to get it dynamically?
Also, is there a way to write out the line number where the error happened
so you can pinpoint exactly where the error occurred?
And if anyone has any suggestions for any other useful information which i
can write to the error log, i'd appreciate it.
Thanks, John

Private Sub subGetTruckerCodes( _
ByRef argDS As DataSet _
)

Try

If sqlConnect.State = ConnectionState.Closed Then
sqlConnect.Open()
End If
sqlConnect.ChangeDatabase("AccountManage")
Dim dad As New SqlDataAdapter("spGetProfileTruckerCodes", sqlConnect)
dad.SelectCommand.CommandType = CommandType.StoredProcedure
dad.SelectCommand.CommandTimeout = 120

Dim ds As New DataSet
dad.Fill(ds)

Catch ex As Exception

Log.Error("subGetTruckerCodes")
Log.Error(ex.Message)

End Try

End Sub

Jun 27 '08 #1
3 1484
"John Walker" <Jo********@discussions.microsoft.comschrieb
Hi, Is there a way to dynamically get the name of the current Sub?
In my code below in the exception "catch" i write the name of the
Sub to the error log so that i will know where there error happened,
but since i reuse this code in other Subs i sometimes forget to
change the name of the Sub. is there a way to get it dynamically?

Dim sf As New StackFrame(True)

Then call sf.GetMethod to get a MethodBase object. It has a Name
property and others.

See also sf.GetFileLineNumber, sf.GetFileName etc. Only available if you
pass True to the constructor and if you also deploy the symbol file
(pdb).

Line number of exception:
- see ex.stacktrace

- Or:

Dim st As New StackTrace(ex, True)
Dim Frame = st.GetFrame(0)
'now Frame.GetFileLinenumber returns the line number

(maybe there's a shorter way but I didn't find one).

Armin

Jun 27 '08 #2
Great, thanks Armin, i'll give it a shot.

"Armin Zingler" wrote:
"John Walker" <Jo********@discussions.microsoft.comschrieb
Hi, Is there a way to dynamically get the name of the current Sub?
In my code below in the exception "catch" i write the name of the
Sub to the error log so that i will know where there error happened,
but since i reuse this code in other Subs i sometimes forget to
change the name of the Sub. is there a way to get it dynamically?


Dim sf As New StackFrame(True)

Then call sf.GetMethod to get a MethodBase object. It has a Name
property and others.

See also sf.GetFileLineNumber, sf.GetFileName etc. Only available if you
pass True to the constructor and if you also deploy the symbol file
(pdb).

Line number of exception:
- see ex.stacktrace

- Or:

Dim st As New StackTrace(ex, True)
Dim Frame = st.GetFrame(0)
'now Frame.GetFileLinenumber returns the line number

(maybe there's a shorter way but I didn't find one).

Armin

Jun 27 '08 #3
John Walker wrote:
Hi, Is there a way to dynamically get the name of the current Sub?
Yes. Several, actually.
In my code below in the exception "catch" i write the name of the Sub to the
error log so that i will know where there error happened, but since i reuse
this code in other Subs i sometimes forget to change the name of the Sub. is
there a way to get it dynamically?
(IMHO) Never, never log /just/ ex.Message. Show it to the user by all
means, but *always* log the whole thing - ex.ToString().

That way, you automagically get not just the /current/ method's name but
that of /every/ method in the call stack at the time the Exception
occurred - and all without /any/ effort on your part.
Also, is there a way to write out the line number where the error happened
so you can pinpoint exactly where the error occurred?
For that, you have to ship the ".pdb" file along with the application's
..exe. With both of these in place, line numbers will be added into the
Exception StackTrace (again, automagically).
And if anyone has any suggestions for any other useful information which i
can write to the error log, i'd appreciate it.
If you need the method name for tracing purposes (for, say, logging the
entry and exit points for every function as the program runs), then you
can use

System.Reflection.MethodBase.GetCurrentMethod().Na me

HTH,
Phill W.
Jun 27 '08 #4

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

Similar topics

4
by: DotNetJunkies User | last post by:
Hi, Does anyone know how/if you can instantiate a C# reference type object dynamically? More specifically, my project has a number of classes that I've created and in some cases it would be very...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
0
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
4
by: assgar | last post by:
Hi I am stuck on a problem. I use 3 scripts(form, function and process). Development on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. The form displays...
3
by: kj | last post by:
I've tried a bazillion ways to code dynamically generated methods, to no avail. The following snippet is a very simplified (and artificial) demo of the problem I'm running into, featuring my...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
11
by: Nadeem | last post by:
Hello all, I'm trying to write a function that will dynamically generate other functions via exec. I then want to be able to import the file (module) containing this function and use it in other...
5
by: akonsu | last post by:
hello, i need to add properties to instances dynamically during run time. this is because their names are determined by the database contents. so far i found a way to add methods on demand: ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...
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.