Announcement

Collapse
No announcement yet.

Who am I? Sub, Function, Method, Property

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Who am I? Sub, Function, Method, Property

    I know I can use FUNCNAME$ and a myriad of other things like callstack. But is there a way for my code to know that the code that is currently running is in a Sub? Function? Method? Property?

    Any PB or API way to do it? or will I need to set some sort of flag to indicate what it is running in?

    Sort of like
    Code:
    Function MyFunction() As Long
         Local CodeType As Whatever
         CodeType = DetermineCodeType(FUNCNAME$)     '<--- Returns value indicating if Sub/Function/Method/Property
    End Function
    Probably I will have to use a flag, but thought I would ask
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    I know there is no way to tell the difference between a SUB and a FUNCTION.

    In subject sample, it's "MyFunction".

    Don't know about methods and properties. What did FUNCNAME$() return when you tried it from a METHOD or PROPERTY call?

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Hi Cliff;

      The description of FUNCNAME$ suggests that it wiil return the the name of the current Sub/Function/Method/Property:

      FUNCNAME$ function
      Purpose
      Return the name of the current Sub/Function/Method/Property.

      Syntax
      f$ = FUNCNAME$

      Remarks
      FUNCNAME$ returns the name of the procedure in which it is located. If an is specified, FUNCNAME$ returns the ALIAS name; otherwise, it returns the primary name capitalized. Returning the ALIAS name provides a mechanism to disguise sensitive internal procedure names even when reporting error conditions to a user.

      FUNCNAME$ can be useful as a debugging tool, or in situations where an error handler in a procedure passes error information on to a "central" procedure for logging and handling. FUNCNAME$ does not require #TOOLS ON.

      See also
      #TOOLS, CALLSTK, CALLSTK$, CALLSTKCOUNT, FILENAME$, FUNCTION, METHOD, PROFILE, PROPERTY, SUB, TRACE

      Example
      SUB SecretEncryptionSub ALIAS "MySub" (sData$)

      x$ = FUNCNAME$ ' Returns "MySub"

      END SUB

      ...

      SUB SecretDecryptionSub (sData$)

      x$ = FUNCNAME$ ' Returns "SECRETDECRYPTIONSUB"

      END SUB

      Comment


      • #4
        what about using a prefix when you name your SUB or FUNCTION

        SUB MySubroutine ALIAS "S_MySubroutine" ()

        FUNCTION MyFunction ALIAS "F_MyFunction" ()

        You can even use:
        "FL_" (for LONG)
        "FD_" (for DOUBLE)
        "FS_" (for SINGLE)
        "FI_" (for INTEGER)
        "FW_" (for WORD)
        "FDW_" (for DOUBLEWORD)
        etc.

        ...
        Patrice Terrier
        www.zapsolution.com
        www.objreader.com
        Addons: GDImage.DLL 32/64-bit (Graphic library), WinLIFT.DLL 32/64-bit (Skin Engine).

        Comment


        • #5
          >what about using a prefix when you name your SUB or FUNCTION..
          >You can even use:
          >"FL_" (for LONG...

          Why not go whole hog, and suffix "@" plus the size on the stack of the parameters, like a bunch of c compilers do when EXPORTing...

          Other than I don't think "@" is allowed in a procedure name, but I'm sure there's a workaround for that.

          (This is not a serious proposal. I am still trying to figure out why I'd even care if FUNCNAME$ was a sub or function or method.... if I can't find a procedure/method name in my source code when debugging I have bigger problems).
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            I am still trying to figure out why I'd even care if FUNCNAME$ was a sub or function or method
            But FWIW, when I was debugging Generic 'ADO' Connection and Query Tester (CC 5+/Win 9+) 11-02-08 , I would just pass "Method <methodname> " or "Property <propertyname>" to a 'common' error handler, and the error log I was creating then included that literal.

            No reason you could not set up something like ..

            Code:
            FUNCTION | SUB  foo... 
              LOCAL dProcTypeName AS STRING 
              dProcTypeName  = " FUNCTION|SUB|METHOD|PROPERTY  "  + FUNCNAME$ 
            
              ....
              code here 
             .. oops need error
              CALL FooError (dProcTypeName, other params) 
            
            ...
            If Funcname$ does in fact return the name of the current method or property , you could do the same kind of thing.

            That is, any procedure receiving FUNCNAME$ would be able to distinguish the "source type" in its own code.



            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment

            Working...
            X