Although VS does a lot of work for you, you won't be able to write a program without knowing the language. It's a common mistake by beginners to learn too less and too late about the language.
I'll try to always have a little practice in this crash course, in fact, during the creation of the demo programs, there'll be a lot of things about the editor or the components offered by the framework. But of course I will talk about more theoretical things, too.
Variables are a central element of every programming language. Only variables make it possible that a program can react on the user's input. There are only a few things in a programming language that are as important as variables.
So, what is a variable? Imagine it as some kind of placeholder that is used when you want to work with a value you don't know exactly. Look at the sentence "Give me half of your bank balance!": bank balance is a placeholder for the exact amount of money in your bank account. It's a variable!
Perhaps you know variables from your math lessons. Look at this equation:
a = b + c
All three letters are variables. a may have a value of 5, b of 3 and c of 2. But we haven't specified that we're using numbers, so a could be "foobar", b "foo" and c "bar".
That's where types come into play. Oxygene is a strongly typed language, which means that it has to be absolutely clear which type one variable has and you can't change that type later. The types used in Oxygene are the types defined in the .NET framework, they are not defined by Oxygene itself.
One very common type is the String type, which holds text of almost arbitrary length. Every character is encoded in UTF-16. For single characters you can use the Char type, which is one UTF-16 encoded character. A String is a sequence of Chars.
To work with numbers, there are several types. For example you can use the type Integer for whole numbers. The Single and Double types are used for real numbers, where Double has twice the precision of the Single type. Both represent floating-point values which will be discussed in an own chapter later.
To get used to working with variables and differente variable types, we will now create a small example application. In order to do that, we have to add a new project to our solution. Please click with the right mouse button on your solution (not the project!) in the solution explorer. Then choose "Add" from the context menu, then "New Project ...". In the already known "New Project ..." dialog, choose once again the "Windows Application" in the "Windows" branch. Call it whatever you want, "SmallCalculator" for example ;-).
Perhaps the old source file of the previous project is still visible after you've added the new project. Just close every tab you don't want to see by either clicking on the cross left of the solution explorer or by clicking right on the tab and selecting "Close". Make sure you're looking at the new, empty form of the new project. If the form isn't visible, doubleclick on the "Main.pas" of the new project in the solution explorer.
Please create a form that looks like the one shown to the right. You can resize the form by clicking and dragging the white dots you see when you select the form by clicking its title. Please use the following names for the components:
- Label "First Number": laFirstNumber, TextBox "First Number": tbFirstNumber.
- Same with "Second Number" and "Result" components.
- Button "Add": btAdd.
Also, find the ReadOnly property of tbResult and set it to true, so no one can type in there, but can copy the result into the clipboard.
Now let's plan what we want to do. We want to take two numbers the user enters in tbFirstNumber and tbSecondNumber, add them and display the result in tbResult.
But that's not as easy as it sounds. The content of a textbox is - surprise - a text of the type String! But we want to add numbers. So, there are a few more steps we have to take: We have to convert the text of both textboxes into numbers (of type Integer), add these numbers and then convert the result into a string. This string can be displayed in tbResult.
Doube click on btAdd to switch to the source code editor.
Declaring a Variable
We'll need some variables to hold the values of the textboxes and the result as numbers. There are two ways to do that: Declare all your variables at the beginning or declare them when you need them. I definitely prefer the second way, but want to show you the first way, too (which is how you have to do it in Delphi for Win32).
The first (old) way looks like this:
method MainForm.btAdd_Click(sender: System.Object; e: System.EventArgs);
var
firstNumber, secondNumber : Integer;
resultNumber : Integer;
begin
A declaration of a variable consists of its name and its type, separated by a colon. The declaration is placed above of the begin of a method and is started with the keyword var, to tell the compiler that declarations of variables are following.
Keywords are a very essential part of every programming language. Their purpose is to build basic constructs of a programming language, such as abegin ...end block or the declaration of variables. Don't use them for your own purposes unless you have a good reason. In that case, you have to prepend them with an ampersand to show the compiler that they're not meant as keywords here.
Multiple variables of the same type can be declared together by first giving their names, separated by commas, and then giving their common type as I did in the first line of declarations. I could've declared resultNumber together with the other two variables, but I wanted to show you that you can declare as many variables as you want without repeating the var keyword.
Choose the names for your variables in a way that you can tell from the name what they are good for. Don't be afraid of longer variable names, if they make sense. But don't be afraid of abbreviations either - if they make sense ;-). You will develop your own style (or have to use a style given to you by your employer).
By the way, the components you put onto the form (textboxes, labels, buttons) are variables, too, but they're declared in a different place by the designer. For example it declared a variable tbFirstNumber : TextBox;. (It's not absolutely correct to call them "variables", because they're declared somewhere else, but it's okay for now.)
Type Inference
So, I've done my duty by showing you the ancient way of declaring a variable. Now, let's look at the easy way. ;-)
If you declare a variable, you want to use it. And when you do so, in most cases the context makes it absolutely clear which type the variables have to have. So, two facts:
- we don't need a variable before we use it the first time.
- the type of variable can be inferred by the context.
That's exactly what Oxygene offers: type inference. For example, firstNumber is supposed to hold the text value of tbFirstNumber as an Integer value. The process of converting a String to something else is also called "parsing". So, look at this:
method MainForm.btAdd_Click(sender: System.Object; e: System.EventArgs);
begin
var firstNumber := Integer.Parse(tbFirstNumber.Text);
(Note that the "old" declaration is gone) What happens here? We declare a variable when we first use it. There's the var keyword again and there's again the name of the variable. The ":=" means that we're assigning a value to a variable (more on that in a few lines). The value comes from a method theInteger type offers, namely the "Parse" method. Methods are discussed later, too, for now: You put something in and in some cases you get something out. In this case we put a String in and get the corresponding Integer back.
And that's the point: Because the compiler "knows", that Parse returns an Integer, we don't have to tell it that again. We don't need the ": Integer" part in the declaration. Nice, isn't it? We not only can declare a variable when we need it, we don't have to care about the type, too.
But don't get confused here! Oxygene is a strongly typed language. Every variable has to have one exactly defined type! We can use firstNumber only(!!) for Integers, because via the return type of the method it's declared as Integer! We don't see the declaration, but the compiler implicitly creates it for us.
Working with Variables
When intoducing type inference, we were a little bit fast, because it was never really explained how to use a variable. We want to catch up with this now. For that, here's the complete source code for the btAdd_Click method.
method MainForm.btAdd_Click(sender: System.Object; e: System.EventArgs);
begin
var firstNumber := Integer.Parse(tbFirstNumber.Text);
var secondNumber := Integer.Parse(tbSecondNumber.Text);
var resultNumber := firstNumber + secondNumber;
tbResult.Text := resultNumber.ToString;
end;
Now, take a look at the first line after "begin". It has a left hand side and a right hand side, separated by the ":=". The := is the assignment operator. It means that the right hand side's value is assigned to the left hand side. The variable on the left side will have the value of the right side after the assignment was executed.
What is the value of the right hand side? It looks a little odd: We've learned that Integer is a type, so what is it doing here? In many languages, type can offer you methods that are useful for working with this type. Like the Parse method. It takes a String and returns an integer value. This is very useful, so the Integer type offers a method for that.
After the first line was executed, firstNumber contains the integer value that corresponds to the numbers typed into tbFirstNumber. The second line of course does the same with the second number.
In the third (non blank) line the result of the addition is calculated on the right hand side and assigned to the variable resultNumber on the left hand side. resultNumber is of type Integer, because both firstNumber and secondNumber are.
The fourth (non blank) line contains the assignment of the result to the Text property of the appropriate textbox. But, of course, we can't assign an integer value to a string property. So, we have to convert it into a string. This is done by calling the ToString method of our resultNumber. If you don't use ToString, you'll get an error message like the one you can see on the right.
Note that every variable of every type has a ToString method. The reason for that will be discussed later, but it's an important fact to know, as it can make your life a lot easier. Every time you want to have a variable as string, just call the variable's ToString method. In many cases you'll get an meaningful string representation.
Finished
Setting the StartUp project
Well, that's all. You can run your calculator program now. Almost ;-)
At the moment, when clicking on the green arrow in the toolbar, the wrong project will be started: the HelloWorld project. Because this project was first added to the solution, this is still the startup project. But we can change this easily. Just choose the menu item "Set as StartUp project" from the context menu of the SmallCalculator project. The name of the project will now be displayed in bold, indicating that it's the startup project.
And now you can click on the green arrow in order to run the calculator project.
Connect with Us