DELPHI DICTIONARY
PART 1 – COMPONENT BASICS
Component Property What it does Extract (Example) Set (Example)
Edit Box .Text Extracts or sets the text displayed in the Edit sName := edtName.Text; edtName.Text := sName;
Box. edtName.Text := ‘Angela’;
Combo Box .Text Extracts or sets the text displayed in the sData := cmbData.Text; cmbData.Text := sName;
Combo Box cmbData.Text := ‘Adhi’;
ComboBox .ItemIndex Returns the index of the user’s selection iSel := cmbData.ItemIndex; cmbData.ItemIndex := 0;
from the ComboBox. Index starts at 0. (Sets selection to first item)
Radio Group .Items Extracts or adds option / option text to Radio sSel := rgpData.Items[rgpItems.ItemIndex]; rgpData.Items.Add(‘Coffee’);
Group. sSel := rgpData.Items[0];
Radio Group .ItemIndex Returns the index of the user’s selection iSel := rgpData.ItemIndex; rgpData.ItemIndex := 0;
from the Radio Group. Index starts at 0. (Sets selection to first item)
List Box .Items Extracts or adds option / option text to List sSel := lstData.Items[lstData.ItemIndex]; lstData.Items.Add(‘Hot Chocolate’);
Box. sSel := lstData.Items[0];
List Box .ItemIndex Returns the index of the user’s selection iSel := lstData.ItemIndex; lstData.ItemIndex := 0;
from the List Box. Index starts at 0. (Sets selection to first item)
Checkbox .Checked TRUE means the box is checked. bMarried := chbMarried.Checked; chbMarried.Checked := TRUE;
FALSE means the box is not checked. (Ticks box)
Label .caption Extracts / sets displayed text sText := lblHeading.Caption; lblHeading.Caption := ‘Hello’;
lblHeading.Caption := sText;
Panel .caption Extracts / sets displayed text sText := pnlWelcome.Caption; pnlWelcome.Caption := ‘Hello’;
pnlWelcome.Caption := sText;
Rich Edit Box .Text Extracts / Clears & Displays text sLine := redOut.Text; redOut.Text := ‘Hello’ + #9 + sName + #13 +
‘How are you?’;
#9 = tabspace; #13 = New Line
Rich Edit Box .Lines.Add Adds a new Line and displays text - redOut.Lines.Add(‘Hello’);
Rich Edit Box .SelText Adds data to existing line in Rich Edit Box - redOut.SelText := ‘Hello’ + #9 + sName;
SpinEdit .Value Sets/Returns selected / typed value iAge := sedAge.Value; sedAge.Value := 16;
StringGrid .Cells Returns or sets values in individual cells sData := stgData.Cells[5, 2]; stgData.Cells[Col, Row] := IntToStr(i);
stgData.Cells[5, 2] := sName;
DateTimePicker .Date Returns/Sets selected date dtDate := dtPurchase.Date; dtPurchase.Date := StrToDate(‘08/10/2018’);
DateTimePicker .Time Returns/Sets selected time dtTime := dtPurchase.Time; dtPurchase.Time := StrToTime(’16:49:20’);
ImageBox .Picture Displays Picture based on filename specified - imgProd.Picture.LoadFromFile(‘RedBull.jpg’);
MemoBox .Text/Lines Same structure as Rich Edit Box Same structure as Rich Edit Box Same structure as Rich Edit Box
DELPHI DICTIONARY
PART 2 – IMPORTANT PRE-DEFINED METHODS – MATH CLASS
Method What It Does Return Type Example
Round Rounds to the nearest whole number. Integer/Real rValue := 5 / 2;
iValue := Round(rValue); // iValue = 3
Frac Returns the fractional part of a number. Real rValue := 10 / 3;
rNew := Frac(rValue); // rNew = 0.3333
Ceil Rounds up a fractional value Integer/Real rValue := 10 / 3;
rNew := Ceil(rValue); // rNew = 4
Floor Rounds down a fractional value Integer/Real rValue := 5 / 3;
rNew := Floor(rValue); // rNew = 1
FormatFloat Converts a Real value to String with Formatting String rValue := 5.253;
sOutput := FormatFloat(‘0.0’, rValue); // sOutput = 5.3
sOutput := FormatFloat(‘R0.00’, rValue); // sOutput = R5.25
Trunc Removes the decimal part of a Real number Integer/Real rValue := 3.55;
rNew := trunc(rValue); // rNew = 3
Mod Divides and returns the remainder of the calculation Integer iRem := 10 mod 3; // iRem = 1
Power Raises a base to an exponent value Real rPow := power(5, 3); // rPow = 125
Sqrt Calculates the Square Root of the provided value Real rSR := sqrt(64); // rSR = 8;
Max Returns the higher of 2 provided values Integer/Real iBig := max(50, 20); // iBig = 50
Min Returns the smaller of 2 provided values Integer/Real iSmall := min(50, 20); // iSmall = 20
Abs Returns the absolute value of a provided number Integer/Real rNum := 30 – 50;
rNum := abs(rNum); // rNum = 20
Pi Returns the value of pi as a constant Real rCircleArea := pi * power(rRadius, 2);
Val Attempts to convert a String to Real. If successful, returns an Real sNum := ‘500’;
error code of 0. val(sNum, rNum, iError); // rNum = 500; iError = 0;
If unsuccessful, returns the position of the error. sNum := ‘5A5’;
val(sNum, rNum, iError); // rNum = undefined; iError = 2
Succ Returns the successor to the provided value Integer iNum := 50;
iNum := succ(iNum); // iNum = 51
Pred Returns the predecessor to the provided value Integer iNum := 49;
iNum := pred(iNum); // iNum = 49
RandomRange Returns a random value from the min to the max – 1 Integer iRan := RandomRange(50, 100); // Range: 50-99
Random Returns a random number from 0 to specified value – 1 Integer iRan := Random(100); // Range: 0-99
Sign Returns 1 if a number is positive / -1 if a number is negative Integer iSign := Sign(-205); // iSign = -1
CompareValue Compares 2 numbers. 1st bigger: 1 / 1st smaller: -1 / Equal: 0 Integer iCom := CompareValue(150, 100); // iCom = 1
DELPHI DICTIONARY
PART 3 – DATABASE CODE CONSTRUCT EXAMPLES
General Structure (Reading) Calculating the Sum/Average Finding Highest
tblExample.First; tblExample.First; tblExample.First;
while not tblExample.EOF do rSum := 0; iHigh := tblExample[‘Age’];
Begin while not tblExample.EOF do while not tblExample.EOF do
Begin begin
tblExample.Next; rSum := rSum + tblExample[‘Age’]; if tblExample[‘Age’] > iHigh then
End; tblExample.Next; begin
End; iHigh := tblExample[‘Age’];
rAve := rSum / tblExample.RecordCount; end;
tblExample.Next;
end;
// Swop sign in IF statement for LOWEST
Updating a Record Adding a Record Deleting a Record
tblExample.Edit; tblExample.Open; tblExample.Delete;
tblExample[‘Name’] := ‘Erica’; tblExample[‘ID’] := IntToStr(tblExample.RecordCount + 1);
tblExample[‘Age’] := 17; tblExample.Append;
tblExample.Post; tblExample[‘Name’] := ‘Ndumiso’;
tblExample.Refresh; tblExample[‘Age’] := 17;
tblExample.Post;
tblExample.Refresh;
Locating a Record Block Adjusting Counting
// Increases everyone’s age by 1 // Counts number of people with mark < 30
sName := edtName.Text;
bFound := FALSE; tblExample.First; iCount := 0;
tblExample.First; while not tblExample.EOF do tblExample.First;
while not tblExample.EOF do Begin while not tblExample.EOF do
Begin tblExample.Edit; Begin
If tblExample[‘Name’] = sName then tblExample[‘Age’] := tblExample[‘Age’] + 1; If tblExample[‘Mark’] < 30 then
Begin tblExample.Post; Begin
bFound := TRUE; tblExample.Refresh; Inc(iCount);
End; tblExample.Next; End;
tblExample.Next; End; tblExample.Next;
End; End;
PART 4 – VALIDATION
Text Only Text / Spaces / Length Digits Only
bValid := TRUE; bValid := TRUE; bValid := TRUE;
for i := 1 to Length(sWord) do for i := 1 to Length(sWord) do for i := 1 to Length(sWord) do
Begin Begin Begin
If not(sWord[i] in [‘A’..’Z’, ‘a’..’z’]) then If not(sWord[i] in [‘A’..’Z’, ‘a’..’z’, ‘ ‘]) then If not(sWord[i] in [‘0’..’9’’]) then
Begin Begin Begin
bValid := FALSE; bValid := FALSE; bValid := FALSE;
End; End; End;
End; End; End;
If bValid = TRUE then… If (Length(sWord) = 13) AND (bValid) then … If bValid = TRUE then…
PART 5 – TEXT FILES
Checking if the file exists Reading from the Text File Creating a new text file
AssignFile(tFile, ‘new.txt’);
If FileExists(‘data.txt) then… AssignFile(tFile, ‘data.txt’); Rewrite(tFile);
Reset(tFile); WriteLn(tFile, sLine);
Checking if the file does not exist While not eof(tFile) do CloseFile(tFile);
Adding to an existing text file
Begin
If not FileExists(‘data.txt’) then… ReadLn(tFile, sLine); AssignFile(tFile, ‘myfile.txt’);
Append(tFile);
WriteLn(tFile, sLine);
End; CloseFile(tFile);
CloseFile(tFile);
DELPHI DICTIONARY
PART 6 – ONE DIMENSIONAL ARRAYS
Declaring an array Populating Array with random values Displaying values vertically
arrNum : Array[1..5] of Integer; For i := 1 to 5 do For i := 1 to 5 do
Begin Begin
Declaring and Initialising values arrNum[i] := RandomRange(1, 101); redOut.Lines.Add(IntToStr(arrNum[i] );
End; End;
arrNum : Array[1..5] of Integer = (10,
50, 40, 100, 80);
Displaying values horizontally Populating Array with user Input Calculating Sum / Average
rSum := 0;
For i := 1 to 5 do For i := 1 to 5 do For i := 1 to 5 do
Begin Begin Begin
edtOut.SelText := IntToStr(arrNum[i]) + #9; arrNum[i] := StrToInt(InputBox(‘Cram’, ‘Enter number’, ‘’)); rSum := rSum + arrNum[i];
End; End; End;
rAve := rSum / Length(arrNum);
Finding Highest Bubble Sort Search
* If there’s parallel arrays * If there’s parallel arrays * If there’s parallel arrays
iHigh := arrNum[1]; For i := 1 to Length(arrNum) do sName := edtName.Text;
sName := arrNames[1]; * Begin iPos := 0;
For j := 1 to Length(arrNum) – 1 do
for i := 2 to 5 do Begin for i := 1 to Length(arrNames) do
begin If arrNum[j] > arrNum[j+1] then begin
if arrNum[i] > iHigh then Begin if sName = arrNames[i] then
begin iTemp := arrNum[j]; begin
iHigh := arrNum[i]; arrNum[j] := arrNum[j+1]; iPos := i;
sName := arrNames[i]; * arrNum[j+1] := iTemp; iMark := arrMarks[i]; *
end; end;
end; sTemp := arrNames[j]; * end;
arrNames[j] := arrNames[j+1]; *
// Lowest – Change sign in IF arrNames[j+1] := sTemp; * If iPos = 0, it means the name was NOT
End; found.
End; If Pos > 0 then it will stores the position in
End; the array of the name.
// Descending – Change sign in IF
Block Adjust “Delete” Reverse Order
Subtract 1 from all numbers Requires a variable iMax which stores number arrTemp should be declared with the same number
of items / type as the original.
of values in Array.
For i := 1 to Length(arrNum) do * If there’s parallel arrays iCount := 1;
Begin For i := Length(arrNum) DownTo 1 do
arrNum[i] := arrNum[i] – 1; iPos := sedDelete.Value; // Position to delete Begin
arrTemp[iCount] := arrNum[i];
End;
inc(iCount);
for i := iPos to iMax – 1 do End;
begin
arrNum[i] := arrNum[i + 1]; For i := 1 to Length(arrNum) do
Begin
arrNames[i] := arrNames[i + 1]; *
arrNum[i] := arrTemp[i];
end; End;
dec(iMax);
PART 7 – BASIC STRING HANDLING METHODS
COPY DELETE INSERT
Copies a substring from a String Removes a substring from a String Adds a substring to an existing String
sSub := copy(sLine, startPos, NoOfChars); Delete(sLine, startPos, NoOfChars); INSERT(sSub, sLine, Position);
EXAMPLE: EXAMPLE:
EXAMPLE:
sLine := ‘magnetic’; sLine := ‘Gears of War 3’;
sLine := ‘Hello’;
sSub := copy(sLine, 4, 3); // “net” Insert(‘drobe’, sLine, 13);
Delete(sLine, 5, 1); // “Hell”
// Gears of Wardrobe 3
DELPHI DICTIONARY
PART 7 – TWO DIMENSIONAL ARRAYS
Declaring a 2D array: Assigning Random Values Assigning values on declaration:
(Example range: 1 – 100)
arrName : Array[1..Rows, 1..Cols] of type arrNum : Array[1..5, 1..4] of Integer = ((10, 12, 5,
For r := 1 to 5 do 25), (59, 29, 92, 11), (11, 22, 44, 66), (10, 9, 8,
EXAMPLE: 7), (88, 59, 83, 82));
Begin
arrNum : Array[1..5, 1..4] of Integer; For c := 1 to 4 do
Begin
When referencing 2D arrays, always
reference the Row (r) first and then the arrNum[r][c] := RandomRange(1, 101);
Column (c). End;
End;
Displaying in StringGrid Displaying in Grid format in a Rich Edit Box: Calculating the sum/ave of Rows:
// Column Headings *Declare arrRowAve based on number of
for c := 1 to 4 do
redOut.SelText := #9 +‘Heading 1’ + #9 + ‘Heading 2’ + #9
begin rows:
+ ‘Heading 3’ + #9 + ‘Heading 4’ + #9 + ‘Heading 5’ + #13;
for r := 1 to 5 do EXAMPLE:
begin For r := 1 to 5 do arrRowAve : Array[1..5] of Real;
StringGrid1.Cells[c, r] := Begin
(IntToStr(arrNum[r][c]) + #9); // Row Headings For r := 1 to 5 do
end; redOut.SelText := ‘Heading ‘ + IntToStr(r) + #9; ** Begin
For c := 1 to 4 do iSum := 0;
end;
Begin For c := 1 to 4 do
redOut.SelText := IntToStr(arrNum[r][c]) + #9;
NOTES: End;
Begin
redOut.SelText := #13; *** iSum := iSum + arrNum[r][c];
*In a StringGrid, the Column reference End; End;
comes first.
*Col/Row 0 contains headings. ** Anything placed at the front of the row should be arrRowAve[r] := iSum;
added here // Divide by number of rows if working out
*** Anything to be placed at the end of the row average – in this case 5
should be added here. End;
Display arrRowAve[r] in Display algorithm
where *** is shown.
Finding highest in entire array Transposing a 2D Array Calculating the sum/ave of Columns:
iHigh := arrNum[1][1]; Declare a new array with number of rows and *Declare arrColSums based on number of rows:
number of columns swopped: EXAMPLE:
arrColAve : Array[1..4] of Real;
for r := 1 to 5 do
arrNew : Array[1..4, 1..5] of Integer;
begin For c := 1 to 4 do
for c := 1 to 4 do for r := 1 to 4 do Begin
begin begin iSum := 0;
if arrNum[r][c] > iHigh then for c := 1 to 5 do For r := 1 to 5 do
begin begin Begin
iHigh := arrNum[r][c]; arrNew[r][c] := arrNum[c][r]; iSum := iSum + arrNum[r][c];
rPos := r; end; End;
cPos := c; end;
arrColAve[c] := iSum;
end; NOTE: Transposing means the rows become // Divide by number of rows if working out
end; columns and the columns become rows. average – in this case 4
end; End;
// rPos stores the row position of // Display
highest value. After normal display algorithm, display
// cPos stores the col position of highest arrColAve using normal 1D array display
value. method.
// For lowest, swop the sign in the IF
statement
// Can be adapted into a Search by
using = in the IF statement and iHigh to
a value being search for.
Swopping rows Swopping Columns Left Diagonal Sum \
SQUARE ARRAYS ONLY
r1 := 2; c1 := 3;
r2 := 4; c2 := 4; iSum := 0;
// Swops rows 3 and 4
// Swops rows 2 and 4
for r := 1 to 5 do
For r := 1 to 5 do begin
For c := 1 to 4 do Begin iSum := iSum + arrNum[r][r];
Begin iTemp := arrNum[r][c1]; End;
iTemp := arrNum[r1][c]; arrNum[r][c1] := arrNum[r][c2];
arrNum[r1][c] := arrNum[r2][c]; arrNum[r][c2] := iTemp;
arrNum[r2][c] := iTemp; End;
End;
Right Diagonal Sum / Block Adjust (Entire Array) Block Adjust (1 Row)
SQUARE ARRAYS ONLY EXAMPLE ADDS 1 to all elements
r := 3; // Increase all values in Row 3 by 10
iSum := 0; for r := 1 to 5 do
begin for c := 1 to 4 do
c := 5;
for c := 1 to 4 do begin
For r := 1 to 5 do begin arrNum[r][c] := arrNum[r][c] + 10;
Begin arrNum[r][c] := arrNum[r][c] + 1; end;
iSum := iSum + arrNum[r][c]; end;
dec(c); End;
End;
PART 8 – OOP
CONSTRUCTOR ACCESSOR (GETTER) MUTATOR (SETTER)
*Declare under Public Always a function with a return type the same as Always a procedure with a parameter of the
the attribute it’s associated with. same type as the attribute it is associated
with.
EXAMPLE:
EXAMPLE:
Attributes: fName, fAge, fID Write an accessor for fAge. EXAMPLE:
Write a mutator for fAge.
Write a parameterised constructor to UNDER PUBLIC:
initialise fName and fID: UNDER PUBLIC:
Function getAge : Integer;
Constructor CREATE(sName : String; Procedure setAge(iAge : Integer);
sID : String); PRESS CTRL + SHIFT + C
PRESS CTRL + SHIFT + C
Under IMPLEMENTATION
Press CTRL + SHIFT + C
UNDER IMPLEMENTATION
Function TClass.getAge : Integer;
Under IMPLEMENTATION Begin Procedure TClass.setAge(iAge : Integer);
Result := fAge; Begin
Constructor TClass.CREATE(sName : End; FAge := iAge;
String; sID : String); End;
Begin
fName := sName;
fID ;= sID;
end;
toString Instantiation Calling a procedure:
Always a function Calls the constructor, creating the object in RAM objLearner.setAge(17);
Returns the attributes in a user-
The object should be declared like a variable: Calling a function:
friendly manner.
objLearner : TLearner;
iAge := objLearner.getAge;
Function TClass.toString : String; objLearner := TLearner.Create(sName, sID);
Begin redOut.Text := objLearner.toString;
Result := ‘Name: ‘ + #9 + fName + #13 +
‘ID:’ + #9 + fID;
End;
PART 9 – CALCULATIONS BASED ON %
Increase rAmount by 15% Decrease rAmount by 15% Get 15% of rAmount
rAmount := rAmount * 1.15; rAmount := rAmount * 0.85; rVAT := rAmount * 0.15;