In this part of the tutorial series, i'll explain you what the variables are used for and how to define them.
You've learned the meanings of every line of the default code for newly created forms in the
previous part of the tutorial series. Now it's time to move on to writing something ourselves.
We will be talking about variables. What are they? Why do we need them? How do we define them? Variables are used for storing some data. Just like objects, variables have a type they belong to. The only difference is that objects belong to classes and variables have so called simple types. So what types do we know?
string: this is a type for storing text data.
integer: stores integer numbers (1, 2, -7, 256 etc.)
real: stores floating point numbers (1.35, -790.008 etc.)
char: stores a single character (a, b, E etc.)
boolean: stores only one of two values: TRUE or FALSE.
array: stores a number of any of the types above. We'll talk about it later.
So why do we need these variables? Let's say you want to read a file when your program starts and save a third line somewhere in the memory. You would define a string variable and assign its value to whatever that third line is.
How do we define variables? Every variable in Delphi has to be defined before it can be used. Meaning we first have to describe what this variable is and only then we're allowed to use it. We define variables like this:
variable_name : variable_type;
other_variable : other_type;
var1, var2, var3 : variables_of_same_type;
As you can see it is very simple. Variable names have some limitations. You should only use latin characters, the "_" symbol and numbers to name them. But you CAN'T use a number as a first character of the name (for example
1variable or
2variable). Valid names would be
var1, var_1, some_variable, _someOtherVariable etc.
Where do we define variables? It depend's on how visible we want them to be. If we want them to be easy accessed in the entire project, define them after the
var statement in a unit. We can also do that by defining a variable in a
public clause of a class. There are also local variables, that are defined in functions and procedures, but we'll talk about them in the next tutorial. So let's define some variables and see the example below to have a better understanding of things.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
private_var : string;
public
public_var,
pub2 : integer;
end;
var
Form1: TForm1;
global_var : boolean;
varWithDefaultVal : string = 'This is a default value';
implementation
end.
Now we can see 6 variables defined in this unit. The first one,
private_var, is defined in
TForm1 class'
private section. It means we can only access it from the same class (you'll understand it better in further tutorials). The two variables
public_var and
pub2 are of the same type, so they can be listed, separated by commas and only in the end assigned a type. In this case, they can store integer numbers and are stored in a
public section of the class
TForm1. That means they can be accessed within the class and outside of it. The variable
Form1 was already defined, it has a type of TForm1 (a class). Next is a
global_var variable of a boolean type (so it can only store true or false). It is called a
global variable, because it can be accessed from other units without any problems. And the last one is a definition of a variable with a default value. As you can see, the default value definition is quite simple. NOTE: only global variables (variables under
var directive) can be defined with default values.
Now let's talk about other type of data storage - constants. As the name says - constants have a fixed value, that cannot be changed. Constants are defined under
const directive like this:
var
Form1: TForm1;
global_var : boolean;
varWithDefaultVal : string = 'This is a default value';
const
default_width = 200;
default_title = 'My first form';
As you can see, there is no
const directive at first, we need to write it ourselves. In this example there are two constants defined globally:
default_width with value
200 and
default_title with value
My first form.
Assigning and calling variables and constants. As we've talked before - constants can only be assigned once (like the example above), and variables can be assigned from various areas of the code that they are visible from. If we want to take the value of a variable or a constant - we just have to write its name. If we want to assign the value to a variable - we write its name, followed by
:= and by its new value.
integer_var := 123;
float_var := 34.12;
string_var := 'some text';
boolean_var := false;
float_var := integer_var;
As you can see, the single equality sign cannot assign a value to the variable (except the case where you define it the first time in the
var clause. You may also notice, that variables can be assigned to each other if their types match. You can also notice, that textual variables (strings) are surrounded by single quotes. What if you need to assign a string value of, for example, the word
don't? It also contains a single quote and Delphi would treat it like the end of the string. That's really bad, but there's a method to avoid this, it's called escaping quotes. If you write two single quotes next to each other, Delphi will treat them both as ONE SINGLE QUOTE and will not end the string there. So here's the example of how to assign the string variable, that contains single quotes:
var1 := 'the don''t word is not so scary after all';
And last, but not the least - how do we assign variables
private_var and
public_var? They are not like the global variables, they are members of the class
TForm1 and cannot be accessed like other variables. If we are in a context where these variables are visible (you will understand what that is in further tutorials), we can assign them by using the object of the same class. Class itself is just a definition. It does absolutely nothing! All the work is done by objects of the class. So in this case we have a class
TForm1 and an object
Form1 that belongs to this class. NOTE: every variable that has a type of a class is called an
object, because it is not a simple variable and we can do much more with it. So to assign a variable, that is a member of this class, we find an object of this class and do that through it. Here's how it's done:
Form1.private_var := 'Jeee, we''ve assigned a private variable';
As you can see, we use a period to access anything, that is stored in a class.
IMPORTANT: when assigning vriables to each other, their types must match. That means you cannot assign a string variable's value to an integer variable. Actually, you can, but not in any way, covered in this tutorial. But as you can see in the above example of various assignments - integer values can be assigned to floats. Why? Simple: float is an integer and even more. Float number does not necessary have to be for example 3.58. It can also be without a floating point (for example 7, 8), it will still be a float, but also an integer. Though you can assign integers to floats, you cannot assign floats to integers, because they might contain floating points and integers do not support them.
So we've learned the basic things about variables in Delphi. In the next tutorial, we'll be learning about procedures and functions.
Click this link if you're ready to continue the journey to Delphi.