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

Reflecting on a basic variable and procedure name.

I have two tasks that I expect will be straight forward to anyone with
reflection skills.

1. To return the the name of an object instance (not type) determined at run
time.

For example, I would like to call a function that takes a basic variable and
writes to the debug window the name of the variable and value.

Dim myNumber As Integer = 12
Dim firstName as String = "Barry"

OutputFieldAndValue(myNumber)
OutputFieldAndValue(firstName)

' Gives something like
myNumber : 14
firstName : Barry

Sub OutputFieldAndValue( _
ByVal field As Object)

Dim pad AS Integer = 10
Dim tabs As String = StrDup(1, Tab)

Dim fieldName As String = "Not yet Implemented"
Dim value As String = field.ToString()

Debug.WriteLine(fieldName.PadRight(pad) & ":" _
& tabs & value.ToString.PadLeft(pad))
End Sub

What could "Not yet Implemented" be replaced with?

2. The name of the current procedure.

Eg
Function FunnyFunction() as Object
Debug.WriteLine( ' some statement that out puts "FunnyFunction" ' )
End Function
Jul 21 '05 #1
2 2169
John Bentley <no*****@nowhere.com> wrote:
I have two tasks that I expect will be straight forward to anyone with
reflection skills.

1. To return the the name of an object instance (not type) determined at run
time.

For example, I would like to call a function that takes a basic variable and
writes to the debug window the name of the variable and value.

Dim myNumber As Integer = 12
Dim firstName as String = "Barry"

OutputFieldAndValue(myNumber)
OutputFieldAndValue(firstName)
Nope, neither of these are going to happen.

When you pass a value, that's all that gets passed - a value. There's
no difference between
OutputFieldAndValue (myNumber);
and
OutputFieldAndValue (12);

as far as the OutputFieldAndValue method is concerned.
2. The name of the current procedure.

Eg
Function FunnyFunction() as Object
Debug.WriteLine( ' some statement that out puts "FunnyFunction" ' )
End Function


You can use the StackTrace class for this. Be aware that the JIT may do
interesting things to stack traces though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
> -------------------------------------------------
Jon Skeet wrote (at this indent level):
------------------------------------------------- John Bentley <no*****@nowhere.com> wrote:
I have two tasks that I expect will be straight forward to anyone
with reflection skills.

1. To return the the name of an object instance (not type)
determined at run time.

For example, I would like to call a function that takes a basic
variable and writes to the debug window the name of the variable and
value.

Dim myNumber As Integer = 12
Dim firstName as String = "Barry"

OutputFieldAndValue(myNumber)
OutputFieldAndValue(firstName)
Nope, neither of these are going to happen.
When you pass a value, that's all that gets passed - a value. There's
no difference between
OutputFieldAndValue (myNumber);
and
OutputFieldAndValue (12);

as far as the OutputFieldAndValue method is concerned.


:(
2. The name of the current procedure.

Eg
Function FunnyFunction() as Object
Debug.WriteLine( ' some statement that out puts "FunnyFunction"
' ) End Function
You can use the StackTrace class for this.


That's the tip off! Thanks once more Jon.
Be aware that the JIT may
do interesting things to stack traces though.


With this awareness firmly *Not* in my mind I then came up with:

Imports System.Diagnostics

Function ThisProcedureName() As String
Dim st As New StackTrace()
Dim sf As New StackFrame()

' The procedure we are interested in is not this one but
' the one that called this one. That's one down on the stack.
sf = st.GetFrame(1)

Return sf.GetMethod.Name
End Function

Sub MyProcedureName()
Debug.WriteLine("Name of this procedure: " _
& ThisProcedureName())
End Sub

Keywords: Current Procedure Name
Jul 21 '05 #3

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

Similar topics

3
by: claus.hirth | last post by:
Does the term 'host variable' cover a variable locally declared in a SQL-PL stored procedure? I am asking this question in the context of the SELECT INTO statement.
14
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic...
4
by: Steve Franks | last post by:
I'm new to ASP.NET and working with the 2.0 beta. As a classic ASP developer I have a lot of code that looks like this: sql = "insert into x values(" sql = sql & val1 & ", " & val2 & ", ' " &...
2
by: John Bentley | last post by:
I have two tasks that I expect will be straight forward to anyone with reflection skills. 1. To return the the name of an object instance (not type) determined at run time. For example, I...
3
by: | last post by:
Hi all, I have a question on reflection Lets say I have a custom object called Address. Now, lets say I have a string variable that holds the name of a variable in the object such as...
7
by: garyusenet | last post by:
This is the first time i've worked with openfile dialog. I'm getting a couple of errors with my very basic code. Can someone point out the errors in what i've done please....
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
3
by: dmorand | last post by:
I'm very new to stored procedures so this is probably something very dumb. I want to pass a name of a table to be created to my stored procedure. I'm using the variable @tableName in the code below...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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...
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.