HOME - - - - - Lazarus Tutorials TOC - - - - - - Other material for programmers

Two little Lazarus programming suggestions

Comments, aka "rems", application.title and a "version" constant

This has good information, and a search button at the bottom of the page

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

This page is "browser friendly". Make your browser window as wide as you want it. The text will flow nicely for you. It is easier to read in a narrow window. With most browsers, pressing plus, minus or zero while the control key (ctrl) is held down will change the texts size. (Enlarge, reduce, restore to default, respectively.) (This is more fully explained, and there's another tip, at my Power Browsing page.)


This is a short "tutorial"... more of a "How to", really.

Discussed herein....


Comments

A programmer should often want to include notes within the code which makes an application do what it does.

There are three ways to make comments in Lazarus code, three ways to make the compiler ignore text that is part of what has been typed into the Source Editor.

(If you think you know as much as you wish to about comments, skip down to the section about the usefulness of the "application.title" property, and of having a "version ID" constant.)

If you are working through my guide to Lazarus, you will have seen one way already, although I said nothing about the relevant lines when they arose. Have a look a the following... do you understand everything after the line saying "private"? The code is an extract from an earlier tutorial.

  TForm1 = class(TForm)
    buQuit: TButton;
    eAnswer: TEdit;
    laQuestion: TLabel;
  private
    { private declarations }
    bNum0,bNum1:byte;
  public
    { public declarations }
  end;

In that code, we declare bNum0 and bNum1.

What difference do you think the line that says....

{ private declarations }

.. makes to the code created by the compiler? Decide your answer before you scroll down!

--

--

--

--

... If you said "The line makes no difference at all.. no code is created because of it"...

--

--

...you are correct! It makes no difference to the code for the application created by the compiler.

That doesn't mean it is pointless. What it does do is to help the human reading the code understand something. In this case the comment shows the human that private declarations (whatever they may be!) should go at that location in the application's code. ("Private declarations" are not something you need to worry about at this stage.)

The squiggly brackets that you see will make anything between them "invisible" to the compiler.

Another name for "a comment" is "a remark", which is the origin of the rather poor English programmers sometimes sink to, when they say, for instance, "That bit has been remmed out."

A comment may run across several lines....

  TForm1 = class(TForm)
    buQuit: TButton;
    eAnswer: TEdit;
    laQuestion: TLabel;
  private
    { Comments are
       put in the application's
       code with squiggle brackets
       such as you see here.}
    bNum0,bNum1:byte;
  public
    { public declarations }
  end;

A comment may also be created by surrounding the text with (* in front of the comment, and *) at the end....

  TForm1 = class(TForm)
    buQuit: TButton;
    eAnswer: TEdit;
    laQuestion: TLabel;
  private
    (* Comments can also be
       put in the application's
       code with bracket-and-asterisk
       used as you see here.*)
    bNum0,bNum1:byte;
  public
    { public declarations }
  end;

Sometimes it is easier just to say "I want the rest of this line, just this line, to be a comment, to be ignored."

You do that with a double forward slash: //

  TForm1 = class(TForm)
    buQuit: TButton;
    eAnswer: TEdit;
    laQuestion: TLabel;
  private
    // private declarations
    bNum0,bNum1:byte;
  public
    { public declarations }
  end;

You do sometimes get several rems in a row...

  TForm1 = class(TForm)
    buQuit: TButton;
    eAnswer: TEdit;
    laQuestion: TLabel;
  private
    // Comments are
    //   put in the application's
    //   code several different
    //   ways.
    bNum0,bNum1:byte;
    //bNum0 and bNum1 will be used to hold
    //   the numbers the user is to add
    //   together.
  public
    { public declarations }
  end;

Probably the most important and valid use of rems is to insert explanatory text like the lines after "bNum0..." above.

Notice the use of indenting to make things easier to read.

Another use is less wise, but we all do it...

Suppose you are working on a program which picks two numbers at random....

bNum0:=random(10);
bNum1:=random(10);

... and now suppose that you wanted, say, bNum1 always to be a 4, just while you did some tests. Here's something you might do, now that you know about using rems...

bNum0:=random(10);
bNum1:=4;//for debugging. Restore next when this
    //no longer needed.
//bNum1:=random(10);

If you thought things were now okay, and you didn't need to force bNum1 to be 4 every time... but you might want to go back to doing it, you would just alter the "//"s...

bNum0:=random(10);
//bNum1:=4;//for debugging. Restore next when this
    //no longer needed.
bNum1:=random(10);

A special case of {comment}

You will encounter comments created with the squiggly brackets in both Lazarus and Delphi which are not (entirely) ignored by the compiler. All such comments have a dollar sigh as the first character in the comment. Don't put a $ there yourself, unless you want to make trouble for yourself, until you know how to make this sort of "comment", which is known as a compiler directive. Here's an example....

{$mode objfpc}

Compiler directives affect how the compiler builds your application. The good news is that you don't have to understant them at this stage, and that Lazarus (and Delphi) will put them in as needed. Eventually, you may well want to fiddle with them, to change what is going on... but you don't need to yet!


The "skVer" constant, and the usefulness of the "application.title" property

Variables were explained in an earlier tutorial.

A constant is a little bit like a variable... but it doesn't vary!

You make up your own names for constants, just as you do for variables. One constant I create and use in the code for every application I write is one I call "skVer" (String "K"onstant), for "version ID". I use it often enough that my spellchecker knows that this is "a word", and doesn't ask if I want to correct it.

I put the "right stuff" into my code (I'll explain it in a moment), and once I've done that, I can "fetch" the current version identifier simply by referring to "skVer" where ever I need the version ID. If the version ID needs to be changed, I make one change in my code, at the point where "skVer" is defined, and everywhere in the code that the version ID is important, the right, new, version ID is accessed.

I keep a date in "skVer". It tells me when the application's code was last modified. I don't have to change it every time I change a single line... I only have to change it as often is needed so that whenever I encounter an instance of my application, I can tell how old that version of it is. If I produce, and save (in backups, for instance) several versions in a single day, say 29 July 11, then the second version is 29 July 11b, the third is 29 July 11c, etc.

To create the constant "skVer", you use the following. You can of course set it to other things! But you can't, elsewhere in the code, change what's inside something defined with const. (I've created another constant ("TaxRate") in what follows, so you can see how it is done.)

For constants which will be needed throughout the unit, declare them just after the uses clause near the top of the code.

const skVer='28 July 11';
      TaxRate=12;

Notice the slightly strange thing: Here, not elsewhere, you use the equals sign for assignment. (Elsewhere the equals sign on its own is a comparison operator. Elsewhere you have to pair the equals sign with a colon to do assignments, e.g. bNum0:=5.

And now two little bonuses for you...

the form's title

As I said, I always have a "skVer" constant in the code for my application. And in the FormCreate handler I always have something like the following. What you see here is the specific instance of the code for an application called "DE23"....

DE23f1.caption:='DE23, Barcode reader, version '+skVer;

I had, as usual, named the form "DE23f1" because the application was called "DE23"

The line of code above causes the title bar of the application's window to say "DE23, version 23 Mar 2010" because, in the application's code, I have... for the moment...

const skVer='23 Mar 2010';

As soon as the application starts up, I can see which version I am running, just by looking at the title at the top of the application's window.

While we're at it, put the following in your FormCreate handler, too. (Again, the example assumes that the program's short-form identifier is "DE23".)

application.title:='DE23';

You want to keep this string short. It will adorn icons and such associated with the app.

That's it for this tutorial!

Forgive an abrupt halt? Or not... (^_^)





            powered by FreeFind
  Site search Web search
Site Map    What's New    Search This search merely looks for the words you enter. It won't answer "Where can I download InpOut32?"

If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service. They offer things for the beginner and the corporation.www.1and1.com icon

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!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS 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 freeware, shareware page.

Link to Lazarus Tutorials main page
How to contact the editor of this page, Tom Boyd


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

AND passes... Valid CSS!

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 .....