CONST statement

Purpose Syntax 1: CONST can create named constants. Do not confuse BCX CONST with C const which is used to signify that a variable's value is not modifiable.


 Syntax 1:
 
 CONST ConstantName = Value

Remarks:

Note well ! Do not append any data type specifier, (% ! # $), to any variable on the right hand side of the CONST statement.


 CONST b% = a

is valid but


 CONST b% = a%

is invalid and will not compile.

Purpose Syntax 2: CONST can create macro definitions.


 Syntax 2:
 
 CONST Expression1 = (Expression2)

Example 1:


 DIM A, B
 CONST CubeInt(C) = ((C) * (C) * (C))
 A = 3
 B = 2007 / CubeInt(A + 1)
 PRINT " True Macros in BASIC ! = ", B

Result:


 True Macros in BASIC ! =  31

Remarks:

Note well ! The macro and the variable names in macro definitions must be parenthesized.

If the parentheses surrounding the right hand expression are omitted, the result will be different, for example,


 DIM A, B
 CONST CubeInt(C) = (C) * (C) * (C)
 A = 3
 B = 2007 / CubeInt(A + 1)
 PRINT "True Macros in BASIC ! = ", B

Result:


 True Macros in BASIC ! =  8016

And if the parentheses surrounding variable names are omitted, the result again will be different, for example,


 DIM A, B
 CONST CubeInt(C) = (C * C * C)
 A = 3
 B = 2007 / CubeInt(A + 1)
 PRINT "True Macros in BASIC ! = ", B

Result:


 True Macros in BASIC ! =  200

And again if the parentheses surrounding both the macro and variable names are omitted, the result yet again will be different, for example,


 DIM A, B
 CONST CubeInt(C) = C * C * C
 A = 3
 B = 2007 / CubeInt(A + 1)
 PRINT "True Macros in BASIC ! = ", B

Result:


 True Macros in BASIC ! =  676

Here is another example of CONST used to create a function-like macro.

 
 CONST RunEx(lpFile, _
       lpParameters, _
         nShowCmd) = _
     ShellExecute(0, _
             "open", _
             lpFile, _
       lpParameters, _
                  0, _
           nShowCmd)

Remarks: To use the above macro you must link with SHELL32.LIB. The following shows how RunEx would be called in the application.

 
 RunEx("notepad.exe", "xxx.txt", 1)


 Syntax 3:
 
 CONST ConstantName = Expression

Example 2:

Here is a way to use CONST to embed NULL in a string.


 $IPRINT_OFF
  CONST Filter = "Icons(*.ico)\0*.ICO\0All files(*.*)\0*.*"
 $IPRINT_ON

Example 3:

If you have a C code snippet that should look like:


 void* F_CALLBACKAPI WaveformCB(void *originalbuffer,
                                     void *newbuffer,
                                          int length,
                                           int param)

it can be translated to BCX like this:

 
 CONST pF_Callback = (Void*)F_CALLBACKAPI

 FUNCTION WaveformCB(originalbuffer AS void PTR, _
                           newbuffer AS void PTR, _
                               length AS INTEGER, _
                                param AS INTEGER) AS pF_Callback

PRIVATE CONST statement

Purpose: PRIVATE CONST creates an integer constant that is local in scope within a SUB or FUNCTION. PRIVATE CONST can not be used with strings or expressions.

Example:


 CALL One()
 CALL Two()

 SUB One()
  PRIVATE CONST ONE = 1
  PRIVATE CONST TWO = 2
  PRINT ONE,TWO
 END SUB

 SUB Two()
  PRIVATE CONST ONE = 3
  PRIVATE CONST TWO = 4
  PRINT ONE,TWO
 END SUB

BCX Console Sample Programs using CONST statement.

S46.bas, S53.bas, S70.bas, S76.bas, S85.bas, S111.bas, S115.bas, S120.bas, S121.bas, S122.bas, S124.bas, S126.bas