Delphi static method of a class returning property value

Posted by mitko.berbatov on Stack Overflow See other posts from Stack Overflow or by mitko.berbatov
Published on 2014-06-10T08:42:45Z Indexed on 2014/06/10 9:25 UTC
Read the original article Hit count: 160

Filed under:
|
|

I'm making a Delphi VCL application. There is a class TStudent where I have two static functions: one which returns last name from an array of TStudent and another one which returns the first name of the student. Their code is something like:

class function TStudent.FirstNameOf(aLastName: string): string;
var i : integer;
begin
  for i := 0 to Length(studentsArray) - 1 do begin
    if studentsArray[i].LastName = aLastName then
    begin
       result := studentsArray[i].FirstName;
       Exit;
    end;
  end;
  result := 'no match was found';
end;

class function TStudent.LastNameOf(aFirstName: string): string;
var i : integer;
begin
  for i := 0 to Length(studentsArray) - 1 do begin
    if studentsArray[i].FirstName = aFirstName then
    begin
       result := studentsArray[i].LastName;
       Exit;
    end;
  end;
  result := 'no match was found';
end;

My question is how can I avoid writing almost same code twice. Is there any way to pass the property as parameter of the functions.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about properties