|
-
April 28th, 2004, 06:05 PM
#1
Getting sub or function name, programmatically
In vb6, Is there a programmatic way of getting the current sub or function name, similar to a form name
that is ...
the name of a form can be found by Me.Name.
Is there an equivalent for a sub or function ..
sub AddTotal(x as integer, y as integer)
' body of sub
msg="An error ocurred in" & sub.name
msgbox msg
end sub
Obviously, this syntax doesn't work, sub.name,
it's just an example.
-
April 28th, 2004, 10:39 PM
#2
You could declare a module or global level variable that will hold the name of the current sub/func. So each sub/func appears to have this name assignment as the first statement in the code.
Busy 
-
April 29th, 2004, 12:19 PM
#3
re:Getting sub or function name, programmatically
If you have to assign the name of the sub or function manually to the global variable that you mentioned, in each sub or function, that defeats the whole purpose of the idea- right ??
The idea is to get the name of the sub/function without having to type in each sub/function name.
If your program has 600 functions, then you have to go through each function and type GlobalNameVar="MyFunc". That approach is pointless.
If VB has the feature where you can acquire the sub/function name using some built-in variable (ie.. me.name-> sub.name, or func.name) then you can add some error handling and logging code that is exaclty the same for each sub or function.
So- at this point, I still don't know if VB has a mechanism to get access to the current sub or function name.
-
April 29th, 2004, 12:38 PM
#4
Who made this thread a 5 star rating?
And what criteria was used for the rating?
At this point all of it sounds pointless to me, since all you have to do is make it a public function or sub in order for Me. to have access to it.
-
April 29th, 2004, 01:29 PM
#5
I *may* have clicked on the rating- accidentally. THe wesbite was very slow, and not responding, or the browser was messed up.
It wasn't my intention to rate this thread at all. Feel free to unrate it.
me.myfunc() doesn't give you programmatic to the "name" (in a string) of the function, only it's address.
function MyFunc()
On Error GoTo global_error
WriteToExecutionLogfile "MyFunc()"
#End If
Exit function
' body of the function is here
' body of the function is here
global_error:
WriteToErrorLogfile "Global Error ocurred in MyFunc", me.name
End
Do you see the 2 strings "MyFunc()", and "Global Error ocurred in MyFunc" ?
Is there a mechanism to get the NAME of the current function (or sub), WITHOUT having to type in the name of each function, manually?
The lines containing WriteToExecutionLogfile() and WriteToErrorLogfilesubs, can be added to every function/sub using search and replace in an editor, if the function name can be accessed as a string.
If not, one would have to go through every sub/function and type in the name manually. We would rather avoid that if possible, because we do want to use the Execution and Error logging features above.
-
April 29th, 2004, 07:50 PM
#6
No.. there is no way.
In other languages like C# it is easiy, but it is simply not possible on VB. I searched long time for a solution because I had a quite big app that used a Audit Engine and I wanted to know what Function called the error.. but I gave up after about 2 months of searching docs and MSDN
greetings UNI
-
April 29th, 2004, 09:42 PM
#7
Re: re:Getting sub or function name, programmatically
Originally posted by cappy2112
If you have to assign the name of the sub or function manually to the global variable that you mentioned, in each sub or function, that defeats the whole purpose of the idea- right ??
The idea is to get the name of the sub/function without having to type in each sub/function name.
If your program has 600 functions, then you have to go through each function and type GlobalNameVar="MyFunc". That approach is pointless.
If VB has the feature where you can acquire the sub/function name using some built-in variable (ie.. me.name-> sub.name, or func.name) then you can add some error handling and logging code that is exaclty the same for each sub or function.
So- at this point, I still don't know if VB has a mechanism to get access to the current sub or function name.
Well, that's not an idea rather a problem in which you are trying to find a solution.. And that's the only solution I can think of .. I did not say that you insert this statement manually, you can create a service program that could do this for you for 600 functions.. Goodluck!
Busy 
-
April 30th, 2004, 08:27 AM
#8
hi,
if i understand your code correct, you only need the function
name in itself, so you can maybe try this: create one class module
(i've called mine FunctionFactory) and within you'll have one private global variable holding the function name and just two properties Get and Set like this:
Code:
Private m_InternalClassName As String
Property Get sInternalFuncName() As String
sInternalFuncName = m_InternalClassName
End Property
Property Let sInternalFuncName(strName As String)
m_InternalClassName = strName
End Property
and in your main code/function in your function body at the very
beginning of the function you set the internal function name and then at e.g. line 1000 use classfunctionfactory.sInternalFuncName to get the function name:
Code:
Option Explicit
Private ff As New FunctionFactory
Private Function myfunc1() As Boolean
ff.sInternalFuncName = "function 1"
'code code
Debug.Print "call from: "; ff.sInternalFuncName
End Function
Private Function myfunc2() As Boolean
ff.sInternalFuncName = "function 2"
'code code
Debug.Print "call from: "; ff.sInternalFuncName
End Function
Private Sub Form_click()
myfunc1
myfunc1
myfunc2
myfunc1
myfunc2
myfunc2
myfunc1
End Sub
hope this helps,
regards,
typecast
-
April 30th, 2004, 02:48 PM
#9
Thanks, but actually your approach is what I want to avoid.
SInce the application is very large, I would have to assign the name of each sub or function to the variable.
I'm trying to avoid doing this, but VB doesn't have a built in feature like Python does, to get the function name, as a string, automatically (without having to manually enter the function name).
-
May 1st, 2004, 06:25 AM
#10
If all you are trying to do is avoid manually typing the names of Subs and Functions, one solution might be to save your code as a .txt file and then write yourself a small read/parse sub/function that will assign each of the names to an array which you could then access in your code. Quite simple really.
If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren
Do canibals not eat clowns because they taste funny? 
-
May 1st, 2004, 08:30 AM
#11
hi,
I'm trying to avoid doing this, but VB doesn't have a built in feature like Python does, to get the function name, as a string, automatically (without having to manually enter the function name).
i don't see a difference between
Me.Name and object.sFunctionName
also there is a difference with your hardcoded string (e.g. "MyFunc"), since you don't need to remember the name of your function on the 1000th line of your code and you don't need to search every time you change your function name. i think the effort is smaller, than parse a file with function names ;-)
kind of regards,
typecast
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|