KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Avoiding a syntax error from an undefined variable in interpreted mode
PRODUCT: 4D | VERSION: 6.5 | PLATFORM: Mac & Win
Published On: May 11, 2001
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

NOTE: The following information applies only to a databases running in interpreted mode, and does not apply to compiled mode. For more information see "Interpreted Mode Versus Compiled Mode" in Chapter 1 of the 4D Compiler Reference Manual.

When you create a variable without assigning an initial value to it, you'll find that the variable is in an undefined state. An attempt to read its value will result in a syntax error.

For example:

If (Var="") ` Check to see if the Text Variable is empty

` Do something

End if

If Var has never had a value assigned to it in this process, the "If" statement will generate a syntax error.

To avoid this problem, you must either assign all variables a value or declare them with a Compiler Command before referencing them for the first time. The Compiler Commands will automatically create an initial value for the variable.

For example:

C_TEXT(Var) => Var = ""
C_STRING(Var) => Var = ""
C_BOOLEAN(Var) => Var = False
C_INTEGER(Var) => Var = 0
C_PICTURE(Var) => Var = 0 bytes

Once the variable has been declared with a Compiler Command, the variable will exist until the application is terminated (interprocess variables), the process ends (process variables), or until the variable is erased with the command CLEAR VARIABLE.

You can also use the command Undefined to check if the variable has been defined. Note that in a compiled database, Undefined will never return true.

If (Undefined(Var)) ` Check to see if the variable has been initialized
` Initialize the variable
End if

If (Var="")

` Do something

End if


For both interpreted and compiled databases, the best practice is to declare and initialize all variables before they are used. This will avoid a host of runtime and compilation errors both syntactical and logical in nature.

For more information see "Why Compile Your Database?" in Chapter 1 of the 4D Compiler Reference Manual.