473,395 Members | 1,706 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 2168
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
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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...

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.