473,800 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 subGetTruckerCo des( _
ByRef argDS As DataSet _
)

Try

If sqlConnect.Stat e = ConnectionState .Closed Then
sqlConnect.Open ()
End If
sqlConnect.Chan geDatabase("Acc ountManage")
Dim dad As New SqlDataAdapter( "spGetProfileTr uckerCodes", sqlConnect)
dad.SelectComma nd.CommandType = CommandType.Sto redProcedure
dad.SelectComma nd.CommandTimeo ut = 120

Dim ds As New DataSet
dad.Fill(ds)

Catch ex As Exception

Log.Error("subG etTruckerCodes" )
Log.Error(ex.Me ssage)

End Try

End Sub

Jun 27 '08 #1
3 1501
"John Walker" <Jo********@dis cussions.micros oft.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.GetFileLineN umber, 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.GetFileLi nenumber 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********@dis cussions.micros oft.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.GetFileLineN umber, 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.GetFileLi nenumber 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.Reflecti on.MethodBase.G etCurrentMethod ().Name

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
20304
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 handy to be able to instantiate them based on a string variable representing their class name. Here's an example of what I'd be looking to do. public object DynamicInstantiation(string className) { return new ; }
5
2878
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 elements in the form, including those that are added dynamically. I use document.getElementsByName('Field Name')to achieve the same.
7
10070
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 main menu instead of showing all possible options and only enabling/disabling certain ones. I have a table that stores the menu item name, parent item (if applicable), display order, etc. so that I can dynamically load my
1
6533
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 java class which creates a xml file based on values entered in dynamic jsp page. Now i want to read all those values entered to xml in my other jsp page. I am able to call values from file in my jsp page. But as dynamic values can be any in no...
0
4075
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 java class which creates a xml file based on values entered in dynamic jsp page. Now i want to read all those values entered to xml in my other jsp page.But as dynamic values can be any in no i don't know how could i populate all those in my jsp...
4
2716
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 multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply.
3
1480
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 latest attempt at this. The idea here is to use __getattr__ to trap any attempt to invoke a nonexistent method, have it return a generic handler called _auto which creates the new method dynamically, invokes it, and "installs" it in the class, so...
2
5116
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 = Windows Forms class B = a singleton hosted within A. B is responsible for dynamically loading classes X, Y, and Z.
11
2584
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 modules, but for some reason it only works using the "import <mod>" syntax, and not "from <modimport *" syntax... i.e. in the latter case, the function is dynamically generated, but not accessible from the importing module. Any ideas on what I can...
5
14577
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: class A(object) : def __getattr__(self, name) : if name == 'test' : def f() : return 'test'
0
9694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10509
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...
0
10281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10256
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
10039
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9095
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...
0
5477
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4152
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
3
2953
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.