Click or drag to resize

Default Function

X#
Assign a default value to a NIL argument.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.21
Syntax
 FUNCTION Default(
	uVar REF USUAL,
	uDefault AS USUAL
) AS VOID
Request Example View Source

Parameters

uVar
Type: Usual
The variable to which the default value will be assigned. uVar must be passed by reference to the Default() function.
uDefault
Type: Usual
The default value to assign.

Return Value

Type: 
Remarks
Default() assigns uDefault to uVar if uVar is equal to NIL.
It is functionally equivalent to either of these code samples:
X#
1IF uVar = NIL
2    uVar := uDefault
3ENDIF
4IF IsNil(uVar)
5    uVar := uDefault
6ENDIF
Instead of Default(@uVar, 0), you could use EnforceNumeric(@uVar), which, besides checking uVar against NIL, calls the error system if uVar is neither numeric nor NIL. You could also use EnforceType() whenever uDefault is the empty value (EmptyUsual()) for its data type; this raises an error if uVar is not a specific data type.
Therefore, instead of calling Default(@uVar, NULL_STRING), you could call EnforceType(@uVar, STRING).
Examples
This example uses Default() to check all parameters in a function:
X#
 1FUNCTION Start()
 2    LOCAL cCA1, cCA2
 3    LOCAL nScore
 4    LOCAL dDate
 5    cCA1 := "CA"
 6    cCA2 := "California"
 7    // nScore and dDate are NIL
 8    CheckArgs(cCA1, nScore, cCA2, dDate)
 9    RETURN TRUE
10FUNCTION CheckArgs(uOne, uTwo, uThree, uFour)
11    Default(@uOne, "CA-VO")
12    // NIL argument is assigned a value:
13    Default(@uTwo, 0)
14    Default(@uThree, NULL_STRING)
15    // NIL argument is assigned a value:
16    Default(@uFour, 0.0.0)
17    RETURN TRUE
Instead of using CheckArgs(), the following example, besides checking for NIL, checks for the correct data types and raises an error if incorrect types are passed:
X#
1FUNCTION CheckArgs2(uOne, uTwo, uThree, uFour)
2    Default(@uOne, "CA-VO")
3    EnforceNumeric(@uTwo)
4    EnforceType(@uThree, STRING)
5    EnforceType(@uFour, DATE)
6    RETURN TRUE
See Also