OBJECT/CLASS Text File
5 steps for USING an object Var // step 1 declare VAR
1. Add Unit name of class under uses // tNames: TextFile;
clsName_u sLine, sName: string;
2. Declare object under private // objName : iPosComma, iAge: Integer;
TName;
3. Instantiate the object in button // begin // step 2 IF FileExists
objName := if FileExists(‘name.txt’) = false then
TName.create(string,real,real) begin - new line - showmessage(‘File not found’);
4. Call methods in use button // sString := exit; - new line - end;
objName.getMethod;
5. Free memory in button // objName.Free; AssignFile(tNames, 'Gamers.txt'); //step 3
Try
Steps for creating an object: GENERAL Reset(tNames)/ Append(tNames)/Rewrite() //
a) File new unit step 4
1. Under uses (self added) under interface // Except – new line -ShowMessage('Error opening
sysUtils ( or Math or any other thing ) file');
2. Under interface // type TName = Exit; -new line- End;
class(TObject) - *GEEN ;
3. Under type // private & public while not EOF(tNames) do // step 5
4. Under private add ATTRIBUTES // fName : begin
string; Readln(tNames, sLine); // step 6
5. Under public add METHODS iPosComma := Pos(';', sLine);
sName := Copy(sLine, 1, iPosComma - 1);
METHODS Delete(sLine, 1, iPosComma);
Mutator ( Set procedures ) iAge := StrToInt(sLine);
1. Step 5 // procedure SetMethod - or Writeln - Writeln(tNames, sName + ' ('
( sString : string ); + IntToStr(iAge) + ')');
2. In procedure // write needed code end;
3. In procedure // fName := sString ; ↑ CloseFile(tNames); // step 7
end;
Accessor (Get functions )
1. Step 5 // function GetMethod : string
;
2. In function // write needed code
3. In function // result := fName;
Auxiliary
1. Step 5 // function DisplayInfo :
string ; - can have (parameters)
2. In function // write needed code
3. In function // result := info ;
4. NB ToString function ( step 5 ) //
ToString : string;
5. In function // result := fName + #9 +
displayInfo
Constructor
1. Step 5 // constructor Create
( sString : string ; iInteger :
integer ; )
2. In constructor // fName := sSstring;
EN fName := iInteger;
Destructor
1. Basically // objName.free;
Connecting data modules Messages
1. File new data module save rename if MessageDlg('Are you sure?', mtConfirmation,
2. ADOcon rename connectionString [mbYes, mbNo], 0) = mrYes then
loginprompt = false
3. ADOtbl rename connection TableName InputBox('Enter Name', 'What is your name?',
Active = true 'Default Name');
4. DataSource rename
5. DBGrid (under frm not dm ) under uses ShowMessage('Hello, ' + Name + '!');
dmName_u datasource
ShowMessage(Format('Name: %s, Age: %d',
SORTING = dmName.tblName.sort := ‘fieldName [name, age]));
ASC/DESC’;
Max Val Min Value
var
iLarge : integer; var
begin iSmall: integer;
with dmVisualArts do begin
begin tblArtEntries.First; with dmVisualArts do
iLarge:= -1; begin
while NOT tblArtEntries.eof do tblArtEntries.First;
begin iSmall := 999999; // Set to a very large initial
if tblArtEntries['LearnerNo'] > iLarge then value
iLarge := tblArtEntries ['LearnerNo'];
next ; while NOT tblArtEntries.Eof do
tblArtEntries.Next; begin
end; //while if tblArtEntries['LearnerNo'] < iSmall then
end; //with iSmall := tblArtEntries['LearnerNo'];
Result:= iLarge + 1;(max)
end; // method tblArtEntries.Next;
end; // while
end; // with
Result := iSmall - 1; // Adjust as needed
end; // method
BUBBLE SORT: BINARY SORTING:
K,iEndCounter : integer; get searchValue
sKeep: string; iPosition = 0
bSwapped: boolean; bFound = False //Boolean value
iLowerBound = 1
begin iUpperBound = iNoElements
iEndCounter := iNumElements - 1; while (iLowerBound <= iUpperBound) and (NOT
repeat bFound) do
bSwapped:= False; begin
for K:= 1 to iEndCounter do iMiddle = (iLowerBound + iUpperBound) DIV
begin 2;
if arrNames[K] > arrNames[K + 1] then if searchValue = arrElements[iMiddle] then
begin begin
sKeep := arrNames[K]; iPosition = iMiddle
arrNames[K] := arrNames[K + 1]; bFound = True
arrNames K + 1] := sKeep; end
bSwapped:= True; else
end; //if if searchValue > arrElements [iMiddle] then
end; //for iLowerBound = iMiddle + 1
Dec(iEndCounter); else
until NOT(bSwapped); iUpperBound = iMiddle - 1
end; end// if
end// if
end // while
GGD KGV
begin begin
iNum1 := StrToInt(edtNum1.Text); iNum1 := StrToInt(edtNum1.Text);
iNum2 := StrToInt(edtNum2.Text); iNum2 := StrToInt(edtNum2.Text);
iLCM := 0;
while (iNum1 mod iNum2) > 0 do Repeat
begin iLCM := iLCM + iNum1
iRemain := iNum1 mod iNum2; Until (iLCM mod iNum2) = 0;
iNum1 := iNum2;
iNum2 := iRemain; lblResult.Caption := 'The LCM between ' +
end; edtNum1.Text + ' and ' + edtNum2.Text + ' is: '
+ IntToStr(iLCM);
lblResult.Caption := 'The GCD between ' +
edtNum1.Text + ' and ' + edtNum2.Text + ' is: ' +
IntToStr(iNum2);
end;