Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can I get info about SUB inside SUB?
#4
(05-08-2023, 04:37 PM)thesolarcode Wrote: Hi,

if code is running inside a SUB, can I somehow get the SUB name?
This would be great to have for debugging or logging.

What mnrvovrfc said is probably right, but I'll share my caveman method of tracing my code and errors, when such features are not readily available, in case it helps...
I'm not saying this is the best method, but it has worked for me in the past. 

Here is the general pattern which should give you the gist. 
(You could use a global shared variable like bplus, I just opted to use local variables to preserve the state for nested calls, etc.)

I should probably look at the built-in debugging features, it can't hurt.

Code: (Select All)
' /////////////////////////////////////////////////////////////////////////////

Sub MySub(param1%, param2%, etc.)
    Dim RoutineName As String : RoutineName = "MySub"
    Dim sError As String : sError = ""
    Dim sOperation As String : sOperation = "(variable declarations)"
    Dim iLoop1%
    Dim MyValue1%
    Dim MyValue2%
    (rest of declarations)
    
    If len(sError)=0 then
        for iLoop1% = param1% to param2%
            sOperation = "MyValue1% = MyFunction$(iLoop1%, etc.)"
            sError = MyFunction$(iLoop1%, etc.)
            If len(sError) > 0 then exit for
        next iLoop1%
    End If
    
    If len(sError)=0 then
        (do next thing)
    End If
    
    Etc.
    
    If Len(sError) > 0 Then
        PrintDebugFile "Error in " & RoutineName & " at " & sOperation & ": " & sError
    End If
    
End Sub ' MySub

' /////////////////////////////////////////////////////////////////////////////

Function MyFunction$(param3%, etc.)
    Dim RoutineName As String : RoutineName = "MyFunction$"
    Dim sError As String : sError = "" ' If routine failed, we return empty string, else return error message.
    Dim sOperation As String : sOperation = "(variable declarations)"
    Dim MyValue3%
    (rest of declarations)
    
    If len(sError)=0 then
        sOperation = "if param3% = 0 then"
        if param3% = 0 then
            sError = "Error in " + RoutineName + ": param3% can't be 0"
        end if
    End If
    
    If len(sError)=0 then
        sOperation = "sError = MySubFunction$(MyValue3%, etc.)"
        sError = MySubFunction$(MyValue3%, etc.)
    End If
    
    If len(sError)=0 then
        (do something else)
        (if some condition fails then put error message in variable sError)
    End If
    
    etc.
    
    If Len(sError) > 0 Then
        PrintDebugFile "Error in " & RoutineName & " at " & sOperation & ": " & sError
    End If
    
    MyFunction$ = sError
End Function ' MyFunction$

' /////////////////////////////////////////////////////////////////////////////

Function MySubFunction$(param4%, etc.)
    Dim RoutineName As String : RoutineName = "MySubFunction$"
    Dim sError As String : sError = ""
    
    If len(sError)=0 then
        '(if some condition fails then put error message in variable sError)
    End If
    
    etc.
    
    MySubFunction$ = sError
End Function ' MySubFunction$
Reply


Messages In This Thread
RE: Can I get info about SUB inside SUB? - by madscijr - 05-08-2023, 06:03 PM



Users browsing this thread: 13 Guest(s)