0% found this document useful (0 votes)
3 views15 pages

It Grade 11 Notes

The document provides an overview of arrays, text files, sorting arrays, functions vs procedures, and databases in Delphi programming. It explains how to declare and manipulate arrays, the importance of using memo components for text files, and the methods for connecting and working with databases. Additionally, it covers sorting algorithms, the differences between functions and procedures, and various coding techniques and best practices.

Uploaded by

jassem.saoud07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

It Grade 11 Notes

The document provides an overview of arrays, text files, sorting arrays, functions vs procedures, and databases in Delphi programming. It explains how to declare and manipulate arrays, the importance of using memo components for text files, and the methods for connecting and working with databases. Additionally, it covers sorting algorithms, the differences between functions and procedures, and various coding techniques and best practices.

Uploaded by

jassem.saoud07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

ARRAYS:

● An array is a variable that is comprised of multiple different items


● In Delphi, the items of an array can only be of one data type such as strings or integers
● An array allows one to store a list of different items
● In an array the first item is stored at index 0, similar to a combo box unless we change it
● Parallel arrays are when 2 arrays have data which relates to one another, meaning when
you change data in one array you have to change the data in another array to ensure
your data doesn’t get mixed up
○ An example is marks being in one array and the names of people being in
another
○ If I change around the order of the marks(index) in the array, the wrong marks
will relate to the wrong people so I also have to change the order(index) of the
names

● Arrays are volatile memory, meaning that they get stored in the ram and are deleted
shortly after
● The length function when looking at an array will give the total number of items stored in
the array, whereas the high function will return the value of the highest array
○ arrNames = ["Tim", "John", "Brian", "Sam", "Dricus", "Adam", "Steve"];
○ The length of arrNames is 7 as there are 7 total items
○ The high of arrNames is 6 as 6 is the highest index (Remember, arrays start at
0!)

● When declaring arrays, we declare them under var, just like how we declare other
variables
○ Var
○ arrPrizes: array [1..5] of string;
■ arr is the prefix for arrays
■ The numbers between the square brackets are the indexes. 1..5 means
the array has a total of 5 items with the index starting from 1 and ending
with 5 so that the first item is stored as item 1 and the last one is stored
as item 5
■ The ‘of’ section after the square brackets is used to declare the data type
of the arrays. Examples are strings, integers, reals ect.

● To assign a value to a certain item index within an array, you assign it under the ‘begin’
similar to how you assign values to other variable types
○ Begin
○ arrPrizes[1] := 'Playstation 5';
○ arrPrizes[2] := 'Vacation';
○ arrPrizes[3] := 'Gaming computer';
○ end
🔍EXTRA NOTE

Remember, when using the Random function, you should always use Randomize;
before it just to ensure that the numbers are truly random

● Randomize;
● iRandom:= Random(5);

● For the index of arrays, you don’t always have to use numbers. Letters work as well
○ arrPrizes : array [‘A’..’C’] of string
■ In this case you could declare values to the item indexes of the array the
same way as you do with numbers
○ arrPrizes[‘A’] := 'Playstation 5';
○ arrPrizes[‘B’] := 'Vacation';
○ arrPrizes[‘C’] := 'Gaming computer';

● Arrays can also be declared as constants. To do this you declare them right at the top of
the page under the private section
○ Const
○ arrMovies : array [1..8] of string = ('Shrek', 'Puss in Boots', 'Up', 'Toy Story',
'Frozen', 'Harry Potter', 'Encanto', 'Detective Pikachu');
■ This section is the same as when declaring a regular array as a variable
type
■ The difference between declaring an array as a variable and as a
constant is that when declaring it as a constant, you also add the values
of the indexes. You do this by using an equals sign and then writing every
item value in circle brackets and separated by commas. Note that due to
this array being a string type the items get quotation marks

● If you don’t know what the value of the lowest item index of an array is you can use the
low function. Alternatively if you don’t know what the highest value of the item index is,
you can use the high function
● These 2 functions can come in handy when using for loops
○ For i := low(arrMovies) to high(arrMovies) do

● You can create a dynamic array using the setlength function


○ SetLength(arrName, length of array);
○ It's important to note that the array will always start at index 0, so to enter a value
into the value you entered as the length of the array, you enter it into the index
which is 1 value smaller
○ For example if you set the arrays length as 8, the highest index is 7 and the
lowest is 0, so there are still 8 items in the array
🔍EXTRA NOTE

If you would like to exit a loop without exiting the entire code, don’t use the exit
function, use break instead.

● For i := 1 to 10
● Begin
● ShowMessage(‘Hello world’)
● Break;
● End;

🔍EXTRA NOTE

If you would like to extract the chosen item in a list box

● You can either equate a variable to a certain item


○ sFruit:= ListBox1.items[0]
○ This returns the first value

● You can also find the index chosen using ItemIndex and use a variable in the
square box
○ iIndex:= ListBox1.itemindex;
○ sFruit:= Listbox.items[iIndex];

● You can also put all of that in 1 line of code


○ sFruit:= ListBox1.items[Listbox1.itemindex];

🔍EXTRA NOTE

● To make font in bold use


○ Lbl1.font.style:= [fsBold];

● To make font italic use


○ Lbl1.font.style:= [fsItalics];

● To change the font type used use


○ Lbl1.font.name: ‘Times New Roman’;
Text Files:
● Text files are a form of permanent storage and can be used to store data for long
periods of time to be used at a later stage
● When loading data from a text file, it's far easier to load it into a memo component as
opposed to a rich edit.
○ This is because rich edits can have different font styles and colors whereas
memo’s only have raw characters
○ Text files also only have raw characters meaning that when you save or load
data into or from a memo component, it will output flawlessly
○ If you try to save characters from a rich edit into a text file, you will get alot of
corrupted characters as the text file doesn’t know how to process fonts or styles

● The LoadFromFile function allows you to automatically load all the lines from a text file
○ memDisplay.Lines.LoadFromFile('Athletics.txt');
○ This will load all the lines from the text file called Athletics.txt

● When trying to save a text file as a variable, you use the TextFile type
○ tTemp : TextFile

● You can assign files to a variable but the variable has to be the right variable type
○ To assign files to a variable you use the AssignFile function
○ AssignFile(tNames,’Gamers.txt.’);
○ This is the variable that you are trying to save the file into
○ This is the name of the file you are trying to save as the variable.
○ NB! The name has to be in quotation marks and needs the .txt extension

🔍EXTRA NOTE

A very useful function one can use when writing code is a try block

A try block is similar to an if else statement but rather tries something and if it doesn’t work it
does something else

● Try
● (Your code);
● Except - This will originally be finally but you need to change it manually
● (Your code);
● end;

Under the try you need to write the code you originally want to output
Under the except you need to write the code you want to output if the code under your try
cannot output
● Before you can read from a file you need to open it
○ You can open a file with reset(file or variable name) to open the file without
changing any of the content
■ You cannot add lines to the file if you open it through this method
○ You can open a file with rewrite(file or variable name) to open a file and erase
all of the content on the file
■ This allows you to add lines from the first line to wherever you want as no
more content will be saved on the file
○ You can open a file with append(file or variable name) to open a file without
clearing any of the content on it
■ This allows you to add lines to the file after the last line

● The EOF function allows you to do code until you reach the end of a file
○ While NOT EOF (tTemp)
○ This is a loop which allows you to loop through the lines of a file until you reach
the end of it and do code every time

● The readln function allows you to read a line from a file into a variable
○ ReadLn(tTemp,sLine);
○ Variable your files saved into
○ Variable you’re trying to read/copy the specific line into

🔍EXTRA NOTE

The difference between the UpCase and UpperCase function is that

● UpCase is used to change a single character to the capital form


● UpperCase is used to change a whole string into the capital form

● To change only a certain line within a text file


○ You can read all the information into a variable
■ Remember that when writing into a string variable, use #10 to make
the information go to the next line
■ sUpdate:= sUpdate + sLine + #10;
■ This is known as a line break
○ Have an if statement to check if the certain line matches up with the information
you would like to change
○ Finally you can rewrite the original file with the new line to add all the new
information
■ Rewrite(tFile,sUpdate);
Sorting Arrays:
Example Data:
Index: 1 2 3 4 5 6 7 8 9 10
Element: 4 7 9 8 2 5 10 1 6 3

● When sorting an array using the selection sort, you need 3 different variables
○ The first variable (i) is used to track the array from the lowest index to the highest
index minus 1
○ The second variable (j) tracks the array from the index of i+1 to the highest index
○ The third variable (iTemp) is used a temporary way to store a number

● To sort an array, one must use nested loops


○ When using nested loops, it's important to remember that the inner loop has to
make a full loop before the outer loop makes a single loop

● You sort string variables in exactly the same way as you would integer values, but the
difference is that the temporary variable has to be a string type

Functions VS Procedures:
● Both Functions and Procedures fall under the broad category of methods
● Examples of methods include:
○ Pos
○ Copy
○ Delete
○ IntToStr
○ Inc

● The main difference between Functions and Procedures is that:


○ Functions will always return a value and have to be assigned to a variable
○ Procedures will only change pre-existing variables

PROCEDURE FUNCTION

Delete Pos

Inc Copy

IntToStr

● You can create your own procedure by declaring procedure NAMEOFPROCEDURE


under the private section
○ Naming a procedure has the same rules as declaring variables
○ Once you have written procedure NAME under private, press CTRL + SHIFT + C
to create the skeleton code of the procedure

● You can set parameters for Procedures


○ To do so you add brackets after the procedure containing the 2 variable
parameters
○ GetValues (sName,sLanguage:string);
○ All parameters have to be filled in order for the program to run, so every time you
call on the procedure, you will have to assign values to the parameters like so:

🔍EXTRA NOTE
You can output a built in sound in your code using the Windows.beep function

● Windows.beep(300,150);
● The first value is the frequency
● The second value is the duration in milliseconds

● You can create your own function by declaring function NAMEOFFunction under the
private section
○ Naming a function has the same rules as declaring variables
○ Once you have written function NAME under private, press CTRL + SHIFT + C to
create the skeleton code of the procedure
○ It's important to note that unlike procedures, you need to declare the variable
type of the function, such as string integer or real

● To assign a value that your function should return, you call on the result function
○ You can assign values to the result as you see fit


○ Here the results are Male if the seventh character of edtID is between 5 and 9,
and female if its between 0 and 4
○ You can then assign this result to a variable, or display it somewhere after you
have called on the function

🔍EXTRA NOTE

You can extract the current date by


● Assigning a variable to the DateToStr(now) function
● sDate:= DateToStr(now)
● DateToStr(now) will return the date in the format DD/MM/YYYY
🔍EXTRA NOTE

To load all the lines from a text file into a rich edit use:
● redOut.lines.loadfromfile(FileName);

Databases:
CONNECTING YOUR DATABASE

● A record is a row
● A field is a column

● When working with databases in Delphi you need 3 components:


○ a TADOconnection
○ a TADOtable
○ a TDataSource

WORKING WITH A DATABASE

● Just like your form has a name, your data module also has a name
● In order to work with a database, you have to write the data modules name in the
uses block of your program

● When using a database, call the database by using with dmNAME do as this will allow
you to work with all the databases within your Data Module

● Similar to how when working with a text file, you need to set the cursor at the start by
using reset, you have to do the same with a database by using .first and you use .next
to go the next line

● Similar to the While Not EOF for text file, you can use While Not tblNAME.EOF to loop
through a table

● You can check a certain field within a table by using square brackets and the field name
inside the
○ This will display the contents of the field based on the specific line which is being
read on the table


○ Here we are checking if the contents within grade field equal edtGrade.text, and
if so we add the contents within the TourMoney field on the same line to our
rTotal

○ IT SHOULD BE NOTED THAT THE SPELLING HAS TO BE EXACTLY THE


SAME AS IT IS IN THE TABLE IN ORDER FOR THE PROGRAM TO WORK AS
INTENDED

● When Inserting data into a database you first call on your data module
○ You then set your database to insert mode
○ You then equate each field to a value

○ NB!!! The field names have to be exactly the same as they appear in the table
○ NB!!! Be sure to double check the data type of the different fields of the
database, as the values you add have to match
○ You then use tblNAME.post to actually add the data to your tables

● When sorting a database, use the .sort function


○ You then equate it to the field you would like to sort by
○ You then state whether you want to sort by ascending(ASC) or
descending(DESC)

● When deleting a record from a database, use the .Delete function


○ It should be noted that this function will delete the record you are currently on
○ To delete a different record use a search algorithm for your database

● If you would like to set the record number that the table is working on to a
specific one, use tbl_Name.RecNo:= Number
○ This will cause the cursor to be on the specific record number you have
entered
🔍EXTRA NOTE

● When working with databases in a test, the database will be connected using
code as opposed to components
● This means you don’t have to call on the data module to work with the specific
tables in the database but can just immediately work with it

Dynamic Objects:

● A dynamic object is an object which is created using code


○ You can create nearly any object using code in delphi

● To create a dynamic object, you first have to declare the object as a global variable
○ When declaring the object as a variable, ensure you are using the right name
for the class according to delphi (TPanel,TButton, e.c.t)

● Once you have declared the dynamic object, you now need to actually create the
object
○ You can create a dynamic object by using the variable you declared earlier
○ The format used when creating a dynamic object is pnlGreet :=
TPanel.Create(frmCashRegister);
■ This is the variable you have declared earlier
■ This is the class type of the component you are creating, NOT the
variable name
■ This is the parent object. A parent object is the object the component you
are creating is placed in. The left and right values are determined by the
object and the size of the object

● After you have created your dynamic object you have to set the properties of the
object
○ Important Values:
■ Parent (Which component the object appears on)
■ Left (How far from the left of the parent component the object should be
placed)
■ Top (How far from the top of the parent component will the object be
placed)
■ Height (how high the object is)
■ Width (how wide the object is)

● Once you are done with your dynamic object you need to free it
○ Freeing a dynamic object is basically like destroying it
○ To free an object use the code: ObjectName.Free;
🔍EXTRA NOTE

● Dynamic objects have an assigned value when they are created


● When freeing a dynamic object you want to set the assigned value to nothing

● We can make use of a dynamic objects assigned value when checking if the
object exists or not
○ We do this by checking that the objects value is not assigned to nil
○ If Assigned(edtChildren) then
○ If Assigned(edtChildren) = nil then

You might also like