MaxScript Sintax

MaxScript code is always embedded into a web page:

Each code line in MaxScript must end with a carriage return. The next line distinguish one set of instructions from another.

Variable declaration

There is no special procedure to declare a variable. They can be created anywhere in the page, but according with xBase rules declaration must be in a statements on a line of code of its own.

Variables can have any name up to 52 characters long, and the only limitation is not to use reserved words. Avoiding to declare variables with the same name of an existing table field is also recommended.

MaxScript uspports the xBase loosen data type model: variable can be declared with any data type and can also assume the NULL value to signify they exist but have no value assigned yet.

Once declared the value assumed by the variable determines its data type, that can change anytime according with new values assigned without noticing or raising error messages.

There is no way to force a variable to keep a determined data type: is up to the programmer to avoid to assign unwanted values (and thus change the variable data type).

To declare a variable is enough to name it and make it follow by an equal sign and the value you want to assign to it, like in this example:

N = 100

This line of code declares a numeric variable and assign a value of 100 to it. Numbers are always considered as integers. The decimal precision to use is set by the standard header in use, and can be modified with the command SET DECIMALS TO n (where n is a convenient number from 0 to 8)

Even if the above statement works it is highly recommended to use the assignment operator := instead that the simple equal sign, that can be confusing when coming to expressions using the equal sign as comparison operator.

The preferred method to declare a variable is this:

N := 100

These are some examples for declaring variables:

Character data type

Date data type

Logic data type

Numeric data type

A := "hello”

D := date()

L := .t.

N := 100

B := A

D := DTOC("01/01/2007”)

N := .f.

M := (N*5)/100

C := A + " baby”

D := date() + 7

M := iif(L,.f.,.t.)

K := M * N + 100

Common conventions

Because of the loosen data model adopted by xBase and MaxScript it can become difficult to know what kind of data a variable is supposed to store, even applying some test on it.

It is recommended to give significant names to variables so to understand what they are supposed to serve and what data type they should store.

The first thing depends only by your fantasy, and to the name you will assign to the variable, but the data type should follow some safe rule. These are defined by DBFree conventions.

Naming variables

By convention variables should be named using the supposed data type as first letter (always lowercase). Variable names should always follow the camel typing, with a mix of lower and uppercase to make the name more readable.

These are some examples:

Character data type

Date data type

Logic data type

Numeric data type

cString

dToday

lOk

nRec

cUserName

dLastLogIn

lLastTime

nRepeatTimes

cStandardDataSet

dStartPayCalculation

lYesOrNot

nMaxAllowConnect

If you decide to follow this convention, knowing what type of value a variable is especting to receive becomes simply a matter of looking the first letter of the name.

For example if you encounter a variable named cDate you know it contains a string

Naming arrays

By convention array names should begin with an "a” and service variables (like counters) should be named with three identical letters, omitting the "n”, like in this example:

ttt := 10
declare aVector[ttt] //-- an array of 10 elements
for nnn := 1 to ttt  //-- nnn is a counter
   ? aVector[nnn]
next

Naming functions

Other naming conventions involve the name of User Defined Functions (UDF).

An UDF can be named any name up to 50 character long; anyway only the first 22 chars are significant, the rest is usually ignored (depending by the operating system on server).

Usually a maximun lenght of 35 chars is recommended. Names should never start with uppercase, and must use camel typing. For example this is a good name for a function, self explanatory and stating clearly that expects a string (table name) and a number(record number) as parameters:

function displayRec( cTable, nRec)
use (cTable)
go (nRec)
return

When a function performs an action of support for another function (i.e. is only an helper) it should be name with the suffix do (lowercase), like in this example:

function listTable(cTable)
use (cTable)
go top
do while not eof()
   doPrint()
   skip
enddo
return

function doPrint()
for nnn=1 to fcount()
   ? fieldcont(fieldname(nnn))
next
return .t.

Naming HTML form elements

The rules seen so far apply for xBase code. When going to HTML there are some other noticeable conventions that you should follow with DBFree:

Data form fields

When using web forms not involving javascript all the fields of the form should be in uppercase and start with the prefix EF_ (short for entryfield), like in this example:

<form>
<input type=”text” value=”John” name=”EF_NAME”>
<input type=”text” value=”Smith” name=”EF_SURNAME”>
<input type=”reset” value=”Reset form” name=”PB_ESC”>
<input type=”submit” value=”Save changes” name=”PB_OK”>
<input type=”hidden” value=”GO” name=”VAR_ACTION”>
</form>

You can see in the example that other form elements are present:

If you code following these conventions chances are that you can access most of the MaxScript code around without any modification.