26
ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Embed Size (px)

Citation preview

Page 1: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

ISM 2110Programming for Business Applications

Lecture 2 - Section 2Delphi and Object Pascal Basic

By Tony Chun-Kuen WONG

Tutorial after 12/09/2002

Page 2: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Object Pascal Language

New Term: An object, like components described earlier, is a binary piece of software that performs a specific programming task.

An object reveals to the user (the programmer using the object) only as much of itself as needed; therefore, using the object is simplified. All internal mechanisms that the user doesn't need to know about are hidden from sight. All this is included in the concept of object-oriented programming.

Page 3: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Pascal Units New Term: A unit is a text file that can be compiled

into a module of code.

A Delphi GUI application will contain at least two units. The project source unit contains the project source code. Project source code units have an extension of DPR.

The second type of unit that a Delphi GUI application always has is the main form's unit. A form unit, as its name implies, is a source code unit with an associated form. This type of unit has a filename extension of PAS.

Page 4: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Project source for a default delphi project

01: program Project1;02: 03: uses04: Forms,05: Unit1 in `Unit1.pas' {Form1};06:07: {$R *.RES}08:09: begin10: Application.Initialize;11: Application.CreateForm(TForm1, Form1);12: Application.Run;13: end.

Page 5: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Blank Pascal Unit

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type

TForm1 = class(TForm)

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.DFM}

end.

Page 6: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Anatomy of a Delphi Unit

The uses list is a list of external units that this unit references

The interface section is the section of a unit in which identifiers exported from this unit are declared. An exported identifier is one that can be accessed by other units in the project.

The implementation section of a unit is the section that contains the actual code for the unit.

Page 7: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

The const Keyword

A unit can optionally have one or more const sections. The const section is designated with the const keyword. The const section describes a list of variables that are known as constants.

A constant is an identifier that cannot change.

Page 8: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Example

unit Unit1;

interface

const

AppCaption = ‘My Funny Program 1.0’;

procedure DoSomething;

implementation

const

BaseX = 20;

BaseY = 200;

procedure DoSomething;

begin

{ Code for DoSomething goes here. }

end;

Page 9: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

The type Keyword

New Term: The type keyword is used to declare new types that your program will use.

example

type TMyArray = array [0..19] of Byte;

Page 10: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

The var Keyword

New Term: The var keyword is used to declare a section of code in which variables are declared.

There are several places you can declare a var section. You can have a var section at the unit level, you can have a var section for a procedure or function, or both. You can even have multiple var sections in a unit.

Page 11: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

unit Unit1;

interface

type

TMyArray = array [0..19] of Byte;

const

AppCaption = ‘My Funny Program 1.0’;

var

X : Integer;

MyArray : TMyArray;

procedure DoSomething;

implementation

const

BaseX = 20;

BaseY = 200;

procedure DoSomething;

begin

{ Code for DoSomething goes here. }

end;

end.

Page 12: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Comments in code

Comments are lines of text in your source code that are there for documentation purposes. Comments can be used to describe what the code does, to supply copyright information, or simply to make a note to yourself or other programmers.

Page 13: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Comments can be designated in as many as three different ways. The following are all valid comments lines:

{ Don't forget to free this memory! }

{ TonyCalculator.Pas Ver 3.01 Copyright (c) TurboTony Software 1987 - 2001

}

(* Jason needs to fix this section of code *)

// This is really good code!

{ This code needs to be reworked later }

Page 14: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

NOTE: If you work in a team programming environment, you might have to read your coworkers' code and vice versa.

Concise comments in the code can save hours of time for any programmer who has to read and maintain another programmer's code. Even if you work in a single-programmer environment, commenting your code is a good idea. You'd be surprised how quickly you forget what code you wrote is supposed to do. Good code commenting can save you and your coworkers hours of time, so don't forget to comment your code!

Page 15: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Variables

Variables have to be declared before they can be used. You declare a variable in a special section of code designated with the var keyword, as described earlier

example 1var X : Integer; { variable X declared as an integer variable } Y : Integer; { variable Y declared as an integer variable }

example 2procedure TForm1.Test;var S : string;begin S := `Hello World!'; Label1.Caption := S;end;

Page 16: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Example 3

X := 100; { ‘X’ now contains the value 100 }

X := X + 50; { ‘X’ now contains the value 150 }

Y := 150; { ‘Y’ now contains the value 150 }

X := X + Y; { ‘X’ now contains the value 300 }

Inc(X); { Increment. ‘X’ now contains the value 301 }

A variable is a location set aside in computer memory to contain some value.

Page 17: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Object Pascal Data Types

New Term: In Object Pascal, a data type defines the way the compiler stores information in memory.

Declaring a Variable

In Object Pascal, you must declare a variable's type before you can use the variable

Some data types are signed and some are unsigned. A signed data type can contain both negative and positive numbers, whereas an unsigned data type can contain only positive numbers.

Page 18: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Data TypeSize in Bytes Possible Range of Values

ShortInt 1 -128 to 127

Byte 1 0 to 255

Char 1 0 to 255 (same as Byte)

WideChar2 0 to 65,535 (same as Word)

SmallInt 2 -32,768 to 32,767

Word 2 0 to 65,535

LongInt 4 -2,147,483,648 to 2,147,483,647

Int64 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Integer 4 Same as LongInt

Page 19: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Data Type Size in Bytes Possible Range of Values

Cardinal 4 0 to 2,147,483,647

Single 4 1.5 * 10-45 to 3.4* 1038

Double 8 5.0 * 10-324 to 1.7 * 10308

Real48 8 same as Double

Extended 10 3.4 * 10-4932 to 1.1 * 104932

Comp 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Currency 8 -922,337,203,685,477.5808 to 922,337,203,685,477.5807

Boolean 1 True or False

Variant 16 Varies

Page 20: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Commonly used Operators

Operator Description Example

Mathematical Operators

+ Addition x := y + z;

- Subtraction x := y - z;

* Multiplication x := y * z;

/ Real number division x := y / 3.14;

div Integer division x := y div 10;

Assignment Operators

:= Assignment x := 10;

Page 21: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Operator Description Example

Logical Operators

and Logical AND if (x = 1) and (y = 2) then ...

or Logical OR if (x = 1) or (y = 2) then ...

Equality Operators

= Equal to if (x = 10) then ...

<> Not equal to if (x <> 10) then ...

< Less than if (x < 10) then ...

> Greater than if (x > 10) then ...

<= Less than or equal to if (x <= 10) then ...

>= Greater than or equal to if (x >= 10) then ...

Page 22: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

example

If (started = True) and (X > 30) then

z := x=y;

Page 23: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

Programming using Delphi

Page 24: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

A sample for Loop

1. Begin with a new application.

2. Place a button on the form.

3. Locate the Memo component on the Standard tab of the Component palette (it should be just to the left of the Button component). Click the button, and then click on the form. A memo will be placed on your form.

4. Make the memo larger by dragging the black sizing rectangle in the lower-right corner of the memo.

5. Double-click the button to create an OnClick event handler for the button. Enter code so that the event handler looks like this:

Page 25: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002

A sample for Loopprocedure TForm1.Button1Click(Sender: TObject);var I : Integer;begin Memo1.Lines.Clear; for I := 0 to 5 do Memo1.Lines.Add(‘This is iteration ‘ + IntToStr(I));

Memo1.Lines.Add(‘'); for I := 5 downto 0 do Memo1.Lines.Add(‘This is iteration ‘ + IntToStr(I));

end;

Page 26: ISM 2110 Programming for Business Applications Lecture 2 - Section 2 Delphi and Object Pascal Basic By Tony Chun-Kuen WONG Tutorial after 12/09/2002