Site Tools


dpp:docs_vars

Variables and Arrays

Variables in D++ are quite easy. There are no variable types; all variables are stored as strings. When you use a variable, D++ will figure out how to use it. For example, when you do a mathematical operation, D++ automatically assume the values are numbers. If in fact they are strings, they will be assigned 0 instead. Another example is string concatenation. Since all variables are strings, their actual values are concatenated.

Declaration

The declaration of variables in D++ is very simple, and you have a lot of syntax freedom. Let's look at a few variable declaration examples:

newvar x, y;
newvar foo=1, doo;
newvar h, k=2, i=4;

As you can see, you can define any number of variables you want seperated by commas, and you can define any of those variables. When you define a variable, the expression is evaluated like any other, so you can mention other variables in the declaration.

Valid Variable Names

Starting with v4.0 of D++, variable names are strictly enforced. Variables can only consist of letters, numbers, an underscore (_) and a hyphen (-). Unlike most languages, variables can begin or end with any of these characters. So you could have a variable named “1var” or “var1”. However, like most of these flukes in D++, they aren't recommended as it's bad programming style.

Usage

You use variables in D++ just as you would use them in most other languages. You can insert them into expressions, put them in functions, or define them in an expression. Here's a few examples on defining these variables:

newvar x, y, z;
x = 1;
y = x + 4;
z = (y - 1) * 2;
screenput z;		'displays '8'

You assign variables with the assignment operator, =, and you can insert other things with it. You can, of course, include strings and functions and all the other goodies in those expressions.

Preset Variables

D++ has a few preset variables which you can use in your applications like any other variable. Of course, you can't assign values to them because they are locked.

Variable Value
dpp.ip Local machine IP address
dpp.host Local machine host name
dpp.systemfolder System Folder
dpp.tick Tick Count (Same as GetTickCount API)
dpp.path Path of current location
dpp.crlf A carriage return
True True (case sensitive)
False False (case sensitive)

Arrays

Starting with v4.0 of D++, arrays are now supported. Arrays work very similar to the ways of other languages, mostly C++. Let's look at a few array statements:

newvar array [], foo[3], zah;
resize array, 4;

zah = 2;
array[zah] = zah;
foo[zah - 1] = zah;

As you can see, arrays are also defined with the newvar statement. The only difference is the ussage of the brackets, [], after each array. In this case, 'array' is a blank array, 'foo' is an array with 4 elements (remember, 0 is an element too!), and 'zah' is a normal variable.

Arrays are used just like other variables. All you have to do is specify the index, which is evaluated so you can insert an expression, and you have a normal variable. You can even use functions with arrays, like the split() function or any other array-based user defined function. However, when you do this, the function must be an array-based function. You cannot use a normal function on a whole array.

newvar arr[3], str;

str = "24.129";

arr = split(str, ".");		'valid, split() is an array function
arr[2] = len(str);		'valid, arr[2] functions like a normal variable
arr = len(str);			'error!  arr is an array!

Resizing Arrays

You can also resize your arrays, so that you can use dynamic arrays. Resizing works vsimilar to VB's ReDim function. (for usage of the resize statement, click here)

newvar arr[];

resize arr, 3;
arr[2] = 3;
resize preserve arr, 4;
screenput arr[2]; 		'displays '3'

When you resize an array without the preserve statement, all the information in the variable is lost. However when you do use it, the information is kept that is still within the bounds.

Back to D++ Documentation Index

dpp/docs_vars.txt · Last modified: 2015-07-30 13:46 by daniel