Sunday, 20 June 2010

Record’s variant

A record can have one or more variant elements or fields. This variant allows data to accessed using one or more different fields. To declare a record contain variant use these statements :

Type

   RecordTypeName = record

      Field_1 : type_1;

      ..

      ..

      Field_n : type_n;

      Case Tag : OrdinalType of

         Constant_1 : ( variant_1);

         Constant_2 : ( variant_2);

      End;

Example record contain variant :

Type

   TCategory = (FullTimer, PartTimer);

   TEmployeeRecord : record

     Name : String(30);

     Case Category : TCategory of

       Full Time : ( Salary : currency;

                              Bonus : currency);

       Part Time : (  Salary : currency);

     End;


Example program :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

type
   TCategory = ( FullTimer, PartTimer );
   TEmployeeRecord = record
       Name : String[30];
       Case Category : TCategory of
          FullTimer : ( Salary : currency;
                                  Bonus : currency);
          PartTimer : ( Payment : currency);
   end;

var
   Employee : TEmployeeRecord;

begin
  with Employee do
    begin
     // assigning fields' values
     Name := 'Albert Einstein';
     Category := FullTimer;
     Salary := 1000;
     Bonus := 100;
     // writing fields' values
     writeln('Name : ',Name);
     writeln('Salary : ',Salary:10:2);
     writeln('Bonus : ',Bonus:10:2);
     // assigning fields' values
     Name := 'Albert Einstein';
     Category := PartTimer;
     Payment := 500;
     // writing fields' values
     writeln('Name : ',Name);
     writeln('Payment : ',Payment:10:2);
    end;
  readln;
end.

You can download the complete code here.

Friday, 11 June 2010

Accessing Record’s Elements

After we declare a record ( previous tutorial ), now we are going to access the elements or fields of the record. The fields in a record can be accessed using this notation :

    RecordName.RecordField

Between the field name and the record name separated by a dot.

Example :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

type
   TRecordItem = record
     Item : string;
     Quantity : integer;
     Price : currency;
   end;
var
   ItemStock : TRecordItem;

begin
  // assigning values to record's fields
  ItemStock.Item := 'USB Flash Disk';
  ItemStock.Quantity := 20;
  ItemStock.Price := 10;
  // writing teh fields' values.
  writeln('Item : ',ItemStock.Item);
  writeln('Quantity : ',ItemStock.Quantity,' pc/s');
  writeln('Price : $ ',ItemStock.Price:10:2 );
  readln;
end.

Using with statement

With statement allows us to access a record field without mentioning the record name. The syntax of with statement is :

 with Record do
   begin
     statements
   end;

Example :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

type
   TRecordItem = record
     Item : string;
     Quantity : integer;
     Price : currency;
   end;
var
ItemStock : TRecordItem;

begin
  with ItemStock do
    begin
      // assigning values to record's fields
      Item := 'USB Flash Disk';
      Quantity := 20;
      Price := 10;
      // writing teh fields' values.
      writeln('Item : ',Item);
      writeln('Quantity : ',Quantity,' pc/s');
      writeln('Price : $ ',Price:10:2 );
    end;
  readln;
end.

You can download the complete code here.

Wednesday, 9 June 2010

Record

Record is a structured data type. It contains some elements or fields, and each field can have a different data type. See the picture below.

How to declare a record ?

You have to declare a record after type clause.


type

    RecordTypeName = record

       Field_1 : DataType;

       .

       .

       Field_n : DataType;

    End;

var

   RecordName : RecordTypeName;

For example, we declare a record named ItemStock that has three fields ( Item, Quantity and Price ).


type

   TRecordItem = record

     Item : string;

     Quantity : integer;

     Price : currency;

   end;

var

   ItemStock : TRecordItem;


First, define a TRecordItem then use it to declare a variable named ItemStock. So the ItemStock variable has three fields : Item, Quantity and Price.

Monday, 31 May 2010

Memory Game

In this project, we are going to create a memory game. I think there are three steps of creating a memory game.

Step 1 : Create the form / user interface

Step 2 : Set the properties of the components

Step 3 : Write the code

Step 1 : Create the form / user interface

First you have to create a form with some image components, a label, a start button, a  timer and an exit button. You can set any width or height properties of your components you use. And arrange them like the picture below or you can use your own arrangement.

Then cover all images with buttons. So your form will be like this :

Step 2 : Set the properties of the components

Now, set the properties of all components we use in this project like tables below :

Form

PropertyValue
CaptionMEMORY GAME
Position

poDesktopCenter

Buttons

ComponentPropertyValue
StartButtonNameStartButton
Caption&Start
ExitButtonNameExitButton
Caption&Exit
Button1 .. Button20Caption?

Label

PropertyValue
CaptionMEMORY GAME

Images

PropertyValue
Stretchtrue

Timer

PropertyValue
Interval1000
Enabledfalse

Step 3 : Write the code

Before we start writing the code, we need to prepare some pictures for this project. I have prepared 30 pictures that you can download (jpg format). Or you can have your own pictures. If your pictures are in jpg format, you have to declare it in the uses clause.

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,       jpeg;

To see the complete code, you can download this project here.

Friday, 28 May 2010

Example program of using BinaryToInteger.DLL

In this example, we are going to create a program that can convert an eight binary data into an integer value using BinaryToInteger.DLL. Before we start, you have to create a user interface like this picture.

Every edit box holds a bit of binary data. So there is a byte binary data (8 bits) that will be converted into an integer value. A binary data holds ‘0’ or ‘1’, so we need to limit the input of each edit box. And the code will be :

procedure TForm1.bit8KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in ['0','1',#8,#13]) then 
    key:=#0
      else
        bit7.SetFocus;
end;

To use BinaryToInteger.DLL, you must declare the function after {$R *.DFM}

{$R *.DFM}
function BinToInt(S : String): integer; stdcall; external 'BinaryToInteger.dll';

Don forget, you must place your BinaryToInteger.DLL in the same folder with this project, or you can place this DLL file in the directory : c:\windows\system.

The code of OnClick even of ConvertButton :

procedure TForm1.ConvertButtonClick(Sender: TObject);
var
  BinaryData : string;
begin
  BinaryData := bit8.Text + bit7.Text + bit6.Text + bit5.Text +
  b it4.Text + bit3.Text + bit2.Text + bit1.Text;
  IntegerValue.Caption := IntToStr(BinToInt(BinaryData));  
end;

You can download the complete code of this example program here.

BinaryToInteger.DLL

Now, we're going to create a DLL file named BinaryToInteger.DLL This DLL is for converting “a character string of binary” into an integer value.

Example :

‘11111110’ = 254

The complete code of BinaryToInteger.DLL (click to download)

library BinaryToInteger;

uses
   SysUtils,
   Classes;

{$R *.RES}

function BinToInt(S : string) : integer; stdcall;
var
  DataLength, i, j, buffer, buffer1 : integer;
begin
  DataLength := Length(S);
  buffer1 := 0;
  for i := 1 to DataLength do
    begin
      buffer := StrToInt(S[i]);
      for j := 1 to DataLength-i do buffer := buffer*2;
      buffer1 := buffer1 + buffer;
    end;
  BinToInt := buffer1;
end;

exports
  BinToInt;

begin
end.

To use this DLL file, you can see the example of using BinaryToInteger.DLL

Wednesday, 26 May 2010

Dynamic Array

Dynamic array is an array that has no fix amount. To declare a dynamic array you can use this syntax :

Array of datatype;

Example :

    Var

       MyArray : array of integer;

This array is dynamic with integer data type. The memory of array is not allocated. To allocate memory of array use SetLength procedure.

Example :

    SetLength(MyArray, 5);

    SetLength(MyArray, 2, 3);

    SetLength(MyArray, 2, 3, 5);

It will allocate an array which has five elements. In this case the index will be 0, 1, 2, 3, 4. And to free the array memory, set nil value to the array. 

Example :

    MyArray := nil;

Example program :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
   i, sum : integer;
   MyArray : array of integer;
begin
  SetLength(MyArray, 5);
  MyArray[0] := 12;
  MyArray[1] := 14;
  MyArray[2] := 16;
  MyArray[3] := 18;
  MyArray[4] := 20;
  sum := 0;
  for i:= 0 to Length(MyArray) -1 do
   begin
     sum := sum + MyArray[i];
   end;
  writeln(sum);
  readln; 
end.

You can download the complete code here.

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.