Beneath this an important point still remains.
A TList is not a common type, because contains methods.
We have primarily to create the declared variable, otherwise any operation we call is impossible.
We use the default Create method to initialize the list, by doing it automatically through an associated event; the form's creation:
procedure TForm1.FormCreate(Sender: TObject); begin //Create the list !!! EmployeeList := TList.Create; end;
Create is so called constructor, which creates a new instance of TList, clears the list and prepares it for use.
Well... let's use it!
procedure TForm1.ButtonAddClick(Sender: TObject); var pEmpTemp : pEmployee; begin //new : Create a new pointer type variable new(pEmpTemp); //TRY DISABLING THIS STRING AND SEE WHAT HAPPENS ! //(pEmpTemp^.Id := .. cannot be assigned i need to create the object) pEmpTemp^.Id := self.findNextFreeId; pEmpTemp^.Name := EditNameNew.Text; pEmpTemp^.Surname := EditSurnameNew.Text; self.addEmployee(pEmpTemp); showmessage('Employee with this info : ' + self.printEmployeeWithId(pEmpTemp^.Id) + ' added into TList.'); //dispose(pEmpTemp); //FREE the created pointer //What happens if i enable this string above? //when i call ButtonShowClick the TList has no values!!! end;
By pressing Add button an event is generated.
The related above code creates a new temporary pointer to Employee record, and fulfills its items.
After that it calls the addEmployee procedure, which receives the pointer as parameter.
We'll concentrate on:
- pEmpTemp^.Id := self.FindNextFreeId;
function TForm1.findNumberOfEmployees(): integer; begin result := EmployeeList.Count; end; function TForm1.findNextFreeId(): integer; var imax :integer; pEmpTemp:pEmployee; begin imax:=self.findNumberOfEmployees-1; //TList starts from a etc... if imax <> -1 then //<> means different from begin //Casting : pEmployee = generic pointer of a TList pEmpTemp := self.EmployeeList[imax]; result := pEmpTemp^.Id +1; end else result := 1; end;
calls another function which returns the EmployeeList items number by using the method Count.
We assign this value less 1 to imax, because the information is necessary to manage the array indexes which start from 0: notice that FindNumberOfEmployees (and FindNextFreeId) is called without any argument, because EmployeeList is the only one we work with.
After creation surely the list has one item with zero index, so that imax becomes -1: this is out of range of our list, and in facts in case it is -1, then function returns a default value 1; otherwise we assign the last list's (generic) pointer to a temporary one of pEmployee.
We wrote here a casting instruction: our pEmployee pointer is promoted to the more abstract one of list.
We'll have in future more occasions to treat it. Now we want to underline that we operate a type transformation to make two pointers compatible.
Finally we increment the Id of temporary record pointed by pEmpTemp, returning this as result.
You can see this is done again by dereferencing pointers.