header image
Home arrow Programming arrow Variables
Variables

A variable is a place keeper used to store values in.
 

Data types used with variables:

Data type Description Abbreviation
and example
String values that are text (i.e. numbers and letters) s (sName)
Integer whole numbers (i.e. no fractions) i (iNumber)
Real whole and fractions (i.e. numbers with digits after the decimal point) r (rAmount)
Char any single character (i.e. text, symbol or number) c (cInitial)
Boolean true or false values b (bValid)


Variable declaration:

Variables are declared after the abbreviation “var”

Variables can be declared globally: They are declared under the var above implementation at the top of the programming code. These variables are valid throughout the program.

Variables can also be declared locally: These variables are declared inside the event handler (the procedure you create yourself by double clicking on a button in design mode for example). You must type in the var as well as the variables themselves. These variables are only valid within this specific event handler or procedure.

For example:

procedure TForm1.btnEnterNumClick(Sender: TObject); 
var 
  iCount : Integer; 

begin 
  ... 
end;

Similar variables can be listed after each other separated by a comma:

var
  sName, sSurname : String;
  iAge, iNum1, iNum2 : Integer;
  rAmount : Real;


Rules for variable (and component) names in Delphi:

- Names can be any length but Delphi uses only the first 255 characters.

- First character must be letter or underscore not a number.

- You cannot use any special characters such as a question mark (?), but you can use an underscore (_).

- No spaces area allowed in the name of a variable.

- Reserved words (such as begin, end, if, program) cannot be used as variables.

- Delphi is case insensitive – it does not matter whether capital letters are used or not. Just make sure the way variables or components are used is consistent throughout the program.

Statistics
Members: 2
News: 41
Web Links: 4
Visitors: 433344