Originally posted by Bud Durland
View Post
Announcement
Collapse
No announcement yet.
Macros and Error Checking
Collapse
X
-
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
Code:FUNCTION PBMAIN() as LONG STDOUT 3.1415926 STDOUT Round(5.46+0.0001,3) END FUNCTION
Leave a comment:
-
-
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)
Leave a comment:
-
-
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?
Leave a comment:
-
-
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
Leave a comment:
-
-
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
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
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
Tags: None
-
Leave a comment: