HOME - - / - - / - - DELPHI COURSE INDEX - - / - - / - - TUTORIALS INDEX - - / - - / - - Other material for programmers

Delphi Course: First steps towards databases... with some other stuff along the way...

This page is information rich, and a has search button at the bottom of the page.

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!



You will probably find the text easier to read if you make your browser window much narrower than usual. You may also want to change your browser's zoom level, to enlarge the text. Opera (at least) lets you change zoom level easily. The text will adapt nicely to the settings you decide give the best results for your needs!

The lines of sample Delphi code in these pages will not "wrap". I.e., if a line is too long to show in the width you have set your browser too, parts of the line will be "off the page". Those lines will still copy/paste properly, at least in Opera. Please feel free to send feedback on the choices I've made! (Will you forgive me for not forcing upon you a column of links on the left and a column of ads on the right?)

You may find typos and rough edges in this. None-the-less, the basic information should be accurate. If something seems wrong, or if you find I've assumed knowledge without explaining it in a previous lesson, please let me know. Please forgive matters of typos, etc. for now. I am not inherently sloppy! The blemishes will be dealt with later.




In this lesson, you make an application which can access data.
--- You develop alternate versions.

You should learn how to...
--- put commends in your sourcecode

You should learn about....
--- Declaring variables
--- Error management
--- Elegance

Delphi language:
--- The "does not equal" test:<>
--- if... then... else

Delphi built in functions:
--- UpperCase ====
This lesson will produce a small program which lets you look up data from inside the computer. Even simple database management programs are much, much more than that, and they let you manage you data far more easily than you'll be able to manage it with the program from this lesson. Nevertheless, this lesson is a start towards working with databases.

Start a new application. Put a button, a label, and an edit box on it. Rename form datab1f1. Save the unit as datab1u1.pas. Save the project as datab1.dpr

Make the button's OnClick handler...
procedure Tdatab1f1.Button1Click(Sender: TObject);
begin
label1.caption:='Not known';
if edit1.text='Madrid' then label1.caption:='Spain';
if edit1.text='Rome' then label1.caption:='Italy';
if edit1.text='Lima' then label1.caption:='Peru';
if edit1.text='Moscow' then label1.caption:='Russia';
if edit1.text='New York' then label1.caption:='USA';
end;
You should now have a program which, rather crudely, lets you look up what country a given city is in. You type the city name into the edit box. Next, you click on the button. If the city is known, the country appears in the label.

The program has one "frill": If you type an unknown city, the label is filled with "Not known". Look at the program, make sure you understand why this is happening. Filling something, as this program does, with a default value, before going on to try to find some better value is a common enough trick. Your applications should always be able to give sensible responses even when users enter nonsense requests. Anticiapting the mistakes users will make is difficult.

Is this a "good" database program? Of course not. Is it helping you get going with Delhi? I hope so! Please feel free to change what is being looked up to something that interests you. You could make a little phone directory, or a program to disclose teacher's nicknames, or, or, or.... If your head isn't full of ideas, then either you need to do a bit more of this, or you're not "the right stuff" to be a programmer. Once you get started, you will find yourself full of "I wonder how I could..." ideas.


For the exercise, we're going to look at another solution to the "What if the user types in a city we don't know?" problem. Make the button's OnClick handler...
procedure Tdatab1f1.Button1Click(Sender: TObject);
var sTmp:string;  //new 1
begin
sTmp:='none found'; //new 2
if edit1.text='Madrid' then sTmp:='Spain'; //modified 3
if edit1.text='Rome' then sTmp:='Italy'; //modified
if edit1.text='Lima' then sTmp:='Peru'; //modified
if edit1.text='Moscow' then sTmp:='Russia'; //modified
if edit1.text='New York' then sTmp:='USA'; //modified
if sTmp<>'none found' then label1.caption:=sTmp; //new 4
if sTmp='none found' then label1.caption:='Not known'; //moved and modified 5
end;
There are several things to notice.

Near the ends of most lines, there now appears a //. Everything after the // is ignored by Delphi when it goes to compile your application. The text after the // is called a comment. Even though they are ignored by Delphi, they are important parts of the program code. Comments, properly used, help you keep track of things... and that help can be very welcome!

_____
The line marked new 1 is a declaration. It says that we will be using a variable called sTmp, and that it will to be able to store string type data.

_____
At line "new 2", we put an initial value into sTmp.

In line "modified 3", and the others like it, we change what is stored in sTmp if we discover that the user has entered the name of a town we know about.

_____
Something to notice about if... then...: The "then" is more a matter of consequence than of timing. The Delphi "then" is like the "then" in the following: "If you study hard, then you will get good marks on the test." The Delphi "then" is not like the "then" in the following: "First we have January, then we have February."

_____
Line "new 4" says "If what is stored in sTmp does not equal the string 'none found' then..." Notice how you say "does not equal" in Delphi (and Pascal). This is another case of using two symbols together to mean one "something". You met ":=" earlier.

_____
Note that we have both "=" and ":=". The equals sign on it's own is used in tests where we are asking if two things are equal, i.e. "if edit1.text = 'Madrid'..." ("if edit1.text equals 'Madrid'...", i.e. if what is in edit1.text is the same as 'Madrid'"). The equals sign is also used just after a colon (":=") in which case the best way to read it is "becomes". We have sTmp:='Spain', which should be read "sTmp becomes 'Spain", or, more fullsomely: The contents of variable sTmp should become 'Spain'.

_____
A quick word about how to type out things like this procedure. (Of course, I hope you just used copy / paste to "write" it into your program, but, if you had to type something like it...)

Start by typing....
if edit1.text='' then sTmp:='';
... by hand.

Use copy paste to make enough copies of that. Then go through and write in the other bits by hand. You're not only saving yourself work, you are also giving yourself protection agains typos.

_____
If you reverse the order of lines "new 4" and "moved and modified 5", the program will not work properly. Work out why. It took me a moment, and is a good example of the sort of silly little thing which can make a nuisance out of itself out of all proportion to its sophistication.

_____
Lines "new 4" and "moved and modified 5" work perfectly well when they are in the right order, but the way they are working are a bit clumsy. Happily, Dlephi provides a solution. You can have...
if sTmp<>'none found'
   then label1.caption:=sTmp
   else label1.caption:='Not known';
This brings us to the question of semicolons, which give many beginnners headaches. Basically: If in doubt, put one in. One of the rare places where a semicolon will cause problems is before an "else". Even after many years of programming, I will usually write what we saw a moment ago as follows. The only difference is the addition of the little comment.
if sTmp<>'none found'
   then label1.caption:=sTmp //no ; here
   else label1.caption:='Not known';

Don't think I'm crazy! I don't present the application above as something that you would want to use for database work. It is valuable, I hope, for helping you get a few more programming ideas into your skull.
Just before we leave this little application, here's another example of making it work better, regardless of that users do. It will also show you something about making your application elegant.

If a user typed MADRID or madrid into our current application, it would respond "Not Known".

A very crude fix would be along the lines of adding....
if edit1.text='MADRID' then sTmp:='Spain';
if edit1.text='madrid' then sTmp:='Spain';
... but even that would fail if the user types MAdrid. (You could argue that the user should learn to type accurately!)

A better answer would be to revise the Madrid test so that the only Madrid line reads....
if uppercase(edit1.text)='MADRID' then sTmp:='Spain';
When the code executes, the computer looks at what is in edit1.text, as it did before. However, before asking if it equals 'MADRID', the computer, because of the "uppercase", converts any letters in the contents of edit1.text to uppercase if they are lowercase. (I.e. makes "capitals" out of "small" letters.). "Uppercase" is built into Delphi, and is always available to you.

Now, you could go throught the program, changing each "if" line the way the Madrid line was changed. And if you did, when the code executed, the contents of edit1.text would be converted to upper case over and over again. It would work... but be inelegant. The following is nicer:
procedure Tdatab1f1.Button1Click(Sender: TObject);
var sTmp,sToMatch:string; //modified 1
begin
sTmp:='none found';
sToMatch:=uppercase(edit1.text); //new 2
if sToMatch='MADRID' then sTmp:='Spain'; //modified
if sToMatch='ROME' then sTmp:='Italy'; //modified
if sToMatch='LIMA' then sTmp:='Peru'; //modified
if sToMatch='MOSCOW'then sTmp:='Russia'; //modified
if sToMatch='NEW YORK' then sTmp:='USA'; //modified
if sTmp<>'none found'
   then label1.caption:=sTmp
   else label1.caption:='Not known';
end;





Click here if you're feeling kind! (Promotes my site via "Top100Borland")
   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search
Ad from page's editor: Yes.. I do enjoy compiling these things for you... I hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's Sheepdog Software (tm) freeware, shareware page.

Link to Tutorials main page Link to Delphi Course index
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....