Announcement

Collapse
No announcement yet.

Macros and Error Checking

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

  • Macros and Error Checking

    I will admit when it comes to Macro's I just don't get it. My only experience with them was in Excel over 10 yrs ago and that was just "Recording" the steps I took to do some calculation.

    If I understand correctly, a macro can be used to "Insert" code into my functions at compile time? If this is the case I have an idea that may or may not work.

    If I have functions like the pseudo-code below
    Code:
    Function DoSomething1() AS LONG
         'Do Something here and return a long
    End Function
    
    Function DoSomething2() AS STRING
         'Do Something here and return a String
    End Function
    Can I add a macro to do something like
    Code:
    macro
         Insert "On Error Goto ErrorHandler" as the 1st line in each function
         Insert "Exit Function" or "Exit Sub" on the line above my label for "ErrorHandler"
         Insert the label "Error Handler"
         Insert whatever error handling routines I need
    end macro
    
    Function DoSomething1() AS LONG
         'Do Something here and return a long
    End Function
    
    Function DoSomething2() AS STRING
         'Do Something here and return a String
    End Function
    so that in essence it would be like I added the code to each Function/Sub myself that the layout would look something like
    Code:
    Function DoSomething1() AS LONG
         ON ERROR GOTO ErrorHandler
         'Do Something here and return a long
         EXIT FUNCTION
    ErrorHandler:
         'Error Handling routines
    End Function
    
    Function DoSomething2() AS STRING
         ON ERROR GOTO ErrorHandler
         'Do Something here and return a String
         EXIT FUNCTION
    ErrorHandler:
         'Error Handling routines
    End Function
    This may be an easy answer, or maybe I am way off base, but had to 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
    A PowerBASIC MACRO is not like an Excel "macro" where you have predefined code snippet that performs an action, it is simply text substitution.

    Example:

    Code:
    Macro ShowText(s)
      #if %def(%PB_CC32)
          PRINT S
          WAITKEY$
      #else
          MSGBOX S
      #endif
    End Macro
     
    Function Pbmain
      ShowText("show this text")
      ShowText("show that text")
    End Function
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3
      Kev,
      If I read you right then Macro in this case I may as well build a Sub? or a Function? (Guessing Sub since I do not think Macro's return anything? unless a variable is set to = some value that is)

      Maybe I still dont get it, but starting to think a macro is nothing more than a sub with a different name?
      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? "

      Comment


      • #4
        Cliff,

        Using a MACRO in code will simply replace the name of the MACRO with it's contents. A few examples:

        Code:
         
        MACRO BIGINTEGER = QUAD
         
        [..]
         
        ' "BIGINTEGER" will be replaced with the contents of the "BIGINTEGER" macro...
        DIM BIG AS BIGINTEGER 
         
         
        MACRO RETURN(X)
          FUNCTION = X
          EXIT FUNCTION
        END MACRO
         
        [..]
         
        ' "RETURN" will be replaced with the contents of the "RETURN" macro...
        FUNCTION PBMAIN
          RETURN(-1)
        END FUNCTION
         
         
         
        MACRO PATHONLY(s) = LEFT$(S, INSTR(-1, S, ANY "\/")-1)
         
        [..]
         
        ' "PATHONLY" will be replaced with the contents of the "PATHONLY" macro...
        s = "C:\AUTOEXEC.BAT"
        ? PATHONLY(s)
        kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

        Comment


        • #5
          The difference between a MACRO and a SUB or FUNCTION is that a macro is not a discreet code module. It is simply text substition in your code. Consider this:

          Code:
          ' Constants can't be floating point, so we do this 
          MACRO Pi = 3.1415926
          
          ' Sometimes, PB's banker's rounding is not appropriate
          MACRO RoundUp(TheNumber) = Round(TheNumber+0.0001,3)
          
          FUNCTION PBMAIN() as LONG
          
            STDOUT Pi
          
            STDOUT RoundUp(5.46)
          
          END FUNCTION
          The above PBMAIN routine will be compiled as if you entered it like so:

          Code:
          FUNCTION PBMAIN() as LONG
          
            STDOUT 3.1415926
          
            STDOUT Round(5.46+0.0001,3)
          
          END FUNCTION
          Hope this helps
          Real programmers use a magnetized needle and a steady hand

          Comment


          • #6
            Let me add three words that may help clarifying what's going on when ...
            Originally posted by Bud Durland View Post
            The difference between a MACRO and a SUB or FUNCTION is that a macro is not a discreet code module. It is simply text substition at compile time in your code.

            Comment

            Working...
            X