Once we wrote all procedures and functions to manage the array we can concentrate on code belonging to visual form's object (and/or to form too). They are doors which to easily communicate our intentions to software with: for example the Add button click, when make an employee addition.
Click is an event, and visual objects are provided of some of them, depending by their nature: generally an event must be characterized by code we want to be executed when event happens.
So check just the Add button click event:
procedure TForm1.ButtonAddClick(Sender: TObject); var tempEmployee: TEmployee; begin //create a new temp employee tempEmployee.Id := self.findNextFreeId; tempEmployee.Name := EditNameNew.Text; tempEmployee.Surname := EditSurnameNew.Text; self.addEmployee(@tempEmployee); showmessage('Employee with this info : ' + self.printEmployeeWithId(tempEmployee.Id) + ' added into array.'); end;
After creation of a new local employee variable (with values), procedure passes its reference to addEmployee procedure we saw before: self.addEmployee(@tempEmployee); .
This is quite clear.
To be conscious of events importance see here
procedure TForm1.EditIdDeleteChange(Sender: TObject); var pTempEmployee : pEmployee; begin pTempEmployee := getEmployee(strtoint(EditIdDelete.Text)); if pTempEmployee <> nil then begin LabelNameDelete.Caption := pTempEmployee^.Name; LabelSurnameDelete.Caption := pTempEmployee^.Surname; ButtonDelete.Enabled := true; end else begin LabelNameDelete.Caption := 'N/A'; LabelSurnameDelete.Caption := 'N/A'; ButtonDelete.Enabled := false; end; pTempEmployee:=nil; end;
This code looks at the chars in EditIdDelete (where the ID appears as number but is text indeed) and convert it into an integer with strtoint(), value used by getEmployee to return a pointer to corresponding ID array's member.
This pointer is dereferenced to get name and surname, which to change the labels with from "N/A" to both of them: contextually the ButtonDelete is enabled or disabled.
Even if pTempEmployee is local and dies with the procedure's end, it is neutralized by assigning the nil value; this means it points to nothing!
Well... this is more or less the core of this article: array management is conceptually quite easy, nevertheless the major is project complexity the most difficult is to get it well working.
Pay attention to pointers, as always: present example (you'll find whole code in next page) is structurally simple to help you in getting the right vision of what is happening; in facts code for events is not perfect and it doesn't cover all possibilities (you'll find this out playing with the application).
By making little changes to code, for example by avoiding a dereference deleting a ^, will result in an error; try it yourself to get confidence.
Errors described in Messages windows are very useful: and if you don't understand their meaning, copy and search on internet: probably somebody had same error in past.
You can download all projects file from here.