Xbasic

Variable Data Types

Description

Xbasic provides built-in data types for declaring variables, functions, and making working with SQL and DBF data easier.

Variable Data Types

The following data types can be used to declare a variable.

A - Any

Variables declared as type A can contain any type of value. The variable can contain any type of data.

dim anyVar as A
anyVar = now()
? anyVar
= 04/21/2017 10:42:17 75 am

? typeof(anyVar)
= "T"

anyVar = "A variable of Any Type can hold *any* type of data"
? anyVar
= "A variable of Any Type can hold *any* type of data"

? typeof(anyVar)
= "C"

anyVar = rand() * 100 + 23
? anyVar
= 102.171752929688

? typeof(anyVar)
= "N"

anyVar = null_value()
?anyVar
= <No data returned>

?typeof(anyVar)
= "Z"

B - Blob

A Blob variable is a "Binary Large OBject" variable. Data in a blob variable is stored as binary data. Any data value can be stored in a Blob. For example:

dim blobObj as B
blobObj = FILE.to_blob("c:\image.jpg")
? blobObj.size()
= 262745

blobObj = "this is some text"
? blobObj.size()
= 17

Several methods are available for working with Blobs. See Blob Methods to learn more.

C - Character

dim charVar as C
charVar = "<p>Are you sure you want to {action}?</p>"

dim action as C
action = "close the window"

? evaluate_string(charVar)
= "<p>Are you sure you want to close the window?</p>"

D - Date

A date, where the date is between 00/00/0000 and 12/31/9999. The length is always 8 characters.

dim dateVar as D
dateVar = now()
? dateVar
= {04/20/2017}

? dateVar + 7
= {04/27/2017}

F - Function Pointer

A Function Pointer variable contains a pointer to a function. It can be used to pass a functions as parameters. A Function Pointer variable is treated as a function. The example below demonstrates creating a function pointer and assigning it the UPPER() function. The funcVar can then be used to call the function it was assigned using (),

dim funcVar as F
funcVar = UPPER
? funcVar("This is a test")
= "THIS IS A TEST"

If the function a Function Pointer is assigned takes one or more arguments, the arguments are passed in when the Function Pointer is invoked:

dim funcVar as F
funcVar = stritran
? funcVar("this is a test","test","string")
= "this is a string"

K - Guid

The K data type is used to create globally unique identifier. It's primarily used when working with database to preserve primary key identifiers in Xbasic scripts.

dim guidVar as K
guidVar = api_uuidcreate()

? typeof(guidVar)
= "K"

? guidVar
= "{1ddf5f1d-62da-4911-84c7-c3bd5dc7a136}"


guidVar2 = {f5ab3018-496a-419f-af71-6c19fec1252b}

? typeof(guidVar2)
= "K"

guidVar2 = {clearly-not-a-valid-guid}
ERROR: Invalid Date/ Time or GUID value

L - Logical

Variables declared as L can have one of two possible boolean values: TRUE .T. or FALSE .F..

dim boolVar as L
boolVar = 1=2
? boolVar
= .F.

boolVar = 1=1
? boolVar
= .T.

N - Numeric

N is used to create a variable that contains a number of up to 19 digits. There is no concept of a NULL value for a numeric variable or field, which by default has an initial value of 0.

dim numVar as N
numVar = ROUND(rand() * 1000,0)
? numVar
= 783

P - Object reference

The P type is used to create an reference to an object (such as a class), or a pointer to a "dot" variable. See Pointer and Dot Variables to learn more.

dim dotVar as P
dotVar.number = rand()
dotVar.today = date()
dotVar.myNameIs = "Sharon Smith"

? dotVar
= myNameIs = "Sharon Smith"
number = 0.69061279296875
today = {04/21/2017}

dim dotClassVar as P
dotClassVar = new Extension::JSON()
dotClassVar.setJSON(property_to_json(dotVar))

? dotClassVar.getLength()
= 3

? dotClassVar.getJson()
= {"number":0.690613,"today":"2017-04-21","myNameIs":"Sharon Smith"}

T - Time

A date/time value that stores the date, hour, minute, seconds and hundredths of a second. The length is always 17 characters.

dim timeVar as T
timeVar = now()
? timeVar
= 04/21/2017 09:55:32 62 am

U - Collection

A collection is an ordered list of key-value pairs. The stored value for a key can be of any type. Collections also have methods - similar to classes - that can be used to manipulate the values in the collection.

dim collection as U
collection.set("today",now())
collection["yesterday"] = date() - 1
collection["tomorrow"] = collection["yesterday"] + 2
collection.set("month","April")
collection.set("year",2017)

? collection.dump("key = value")
= today = CTODT('04/21/2017 09:54:37 15 am')
yesterday = {04/20/2017}
tomorrow = {04/22/2017}
month = April
year = 2017

You can use array syntax collection[key] to set or get values in a collection. The set() and get() methods for a collection can also be used to create and read data in a collection. Additional methods are available for sorting, searching, and manipulating the data in a collection. See Collection Methods for more information.

Y - Short Time

A time value that stores the hour, minute, seconds and hundredths of a second. The length is always 9 characters.

dim shorttimeVar as Y
shorttimeVar = now()
? shorttimeVar
= 10:46:10 45 am

Other Data Types

V - Void

The V data type is used when declaring Xbasic functions to indicate that a function does not return anything.

function writeLog as V (msg as C)
    save_to_file(msg,"C:\log.log",.t.)
end function

V cannot be used to DIM variables. If you try to DIM a variable as type V, an Xbasic error will occur:

dim voidVar as V
ERROR: Invalid variable type

Z - NULL

The Z data type is used to indicate a NULL value. It is primarily used when working with databases to preserve NULL values in a record. It is also used to assign a variable to it's NULL value. For example:

? typeof(null_value())
= "Z"

dim charVar as c
charVar = "hello"
? charVar
= "hello"

charVar = null_value()
?charVar
= ""

To test if a variable is NULL, it can be compared to null_value(). For example:

dim charVal as C
? charVal
= ""

? charVal == null_value()
= .T.
Not all variable data types have a NULL value. See Null Table Field Values for a list of supported NULL values for data types.

Z cannot be used to DIM variables. If you try to DIM a variable as type Z, an Xbasic error will occur:

dim nullVar as Z
ERROR: Invalid variable type

DBF Data Types

There are several DBF table field types, such as exponent numeric ("E"), image reference ("I"), JPEG image ("J"), memo ("M"), OLE ("O"), and rich text memo ("R") that are not data types. These data types cannot be used to DIM variables.

Declaring Variables

Once a variable's data type is determined, it cannot be changed. However, you can use the DELETE command to delete a variable and then use the DIM command to redefine it as a different type.

dim var[5] as N = rand()
? var
= [1] = 0.934051513671875
[2] = 0.957061767578125
[3] = 0.6011962890625
[4] = 0.887542724609375
[5] = 0.289764404296875

var = "12"
ERROR: Variable type mismatch: Cannot assign data of type 'C' to variable of type 'A'.

DELETE var

var = DTOC(now())

? var
= "04/21/2017"

? typeof(var)
= "C"
As a best practice, you should not name variables with the names of data types. For example, do not name a field that contains a date value "Date". However, Xbasic is lenient will not enforce this practice.

See Also