Reflecting on a basic variable and procedure name.

  • Thread starter Thread starter John Bentley
  • Start date Start date
J

John Bentley

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
 
John Bentley said:
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 wrote (at this indent level):
-------------------------------------------------
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.
:(


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
 
Back
Top