Tweet RSS

Example Delphi Basic Arrays:

Div
unit TwoDArrayu;
  //By: Jeremy Paton
  //A Jesar|Arts resource
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, jpeg, ExtCtrls;
  Const Column: Array[1..4] of string = ('Joburg','Tswane','Durban','Cape Town');
  Const Row: Array[1..3] of string = ('Mon','Wed','Fri');
  //Constant arrays used in this particular program
type
  TNumbers =Array[1..10,1..3] of integer;
  //Above is the declaration of a 2D array: [1..10] are # of columns and [1..3] are # of rows
  TFrmTwoArray = class(TForm)
    StgOutput: TStringGrid;
    BmbClose: TBitBtn;
    BtnInput: TButton;
    BtnOutput: TButton;
    BtnTotal: TButton;
    ImgSplash: TImage;
    procedure FormCreate(Sender: TObject);
    procedure BtnInputClick(Sender: TObject);
    procedure BtnOutputClick(Sender: TObject);
    procedure BtnTotalClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmTwoArray: TFrmTwoArray;
  Input: Tnumbers;
  Outer: Integer;
  Inner: Integer;
  add: Integer;
implementation
Div

Populating an Array:

Div
procedure TFrmSorted.mmInputClick(Sender: TObject);
begin
  Cnt:= 1; 
    //Counter variable used to keep track of size of array and create a new position in the   array for each input.
  Names[cnt]:= inputbox('Please Enter:','A Name. (Use "end" to stop)','Jeremy');  
    //Inputing through an input box into the first position of the array.
    While lowercase(Names[cnt]) <> 'end' do //The conditional loop
      begin
        inc(cnt);
        Names[cnt]:= inputbox('Please Enter:','A Name. (Use "end" to stop)','Jeremy');
      end;
      dec(cnt)
end;

Starting Intermediate Delphi:

<< Previous Tutorial

>> Next Tutorial

Div

Current Tutorial Info:

When declaring an array of any type (String, Integer, Boolean, Class, etc), this must be declared just under Type and above the form declaration.  

When declaring a constant array, which in principle is the same as declaring any other array, also requires one to populate the array in the same statement.

Almost always when populating a 2D array one will need a form of a loop know as an inner/outer or outer/inner loop.