CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2003
    Posts
    322

    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.

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    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

  3. #3
    Join Date
    Apr 2003
    Posts
    322

    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.

  4. #4
    Join Date
    Jun 2003
    Location
    Planet Earth
    Posts
    186
    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.

  5. #5
    Join Date
    Apr 2003
    Posts
    322
    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.

  6. #6
    Join Date
    Apr 2004
    Location
    Austria
    Posts
    43
    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

  7. #7
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    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

  8. #8
    Join Date
    Apr 2004
    Posts
    17
    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

  9. #9
    Join Date
    Apr 2003
    Posts
    322
    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).

  10. #10
    Join Date
    Jan 2001
    Posts
    486
    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?

  11. #11
    Join Date
    Apr 2004
    Posts
    17
    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
  •  





Click Here to Expand Forum to Full Width

Featured