Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

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

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

4 Project source for a default delphi project 01: program Project1; 02: 03: uses 04: Forms, 05: Unit1 in `Unit1.pas' {Form1}; 06: 07: {$R *.RES} 08: 09: begin 10: Application.Initialize; 11: Application.CreateForm(TForm1, Form1); 12: Application.Run; 13: end.

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

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

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

8 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;

9 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;

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

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

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

13 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 }

14  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!

15 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 1 var X : Integer; { variable X declared as an integer variable } Y : Integer; { variable Y declared as an integer variable } example 2 procedure TForm1.Test; var S : string; begin S := `Hello World!'; Label1.Caption := S; end;

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

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

18  Data TypeSize in BytesPossible Range of Values  ShortInt1-128 to 127  Byte10 to 255  Char10 to 255 (same as Byte)  WideChar20 to 65,535 (same as Word)  SmallInt2-32,768 to 32,767  Word20 to 65,535  LongInt4-2,147,483,648 to 2,147,483,647  Int648-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807  Integer4Same as LongInt

19  Data TypeSize in BytesPossible Range of Values  Cardinal40 to 2,147,483,647  Single41.5 * 10 -45 to 3.4* 10 38  Double85.0 * 10 -324 to 1.7 * 10 308  Real488same as Double  Extended103.4 * 10 -4932 to 1.1 * 10 4932  Comp8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807  Currency8-922,337,203,685,477.5808 to 922,337,203,685,477.5807  Boolean1True or False  Variant16Varies

20 Commonly used Operators  OperatorDescriptionExample  Mathematical Operators  +Additionx := y + z;  -Subtractionx := y - z;  *Multiplicationx := y * z;  /Real number divisionx := y / 3.14;  divInteger divisionx := y div 10;  Assignment Operators  := Assignment x := 10;

21  OperatorDescriptionExample  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...

22 example If (started = True) and (X > 30) then z := x=y;

23 Programming using Delphi

24 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:

25 A sample for Loop procedure 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;

26


Download ppt "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."

Similar presentations


Ads by Google