Delphi Static Class
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. You can use an anonymous method with variable capture for this linear search. This approach gives you complete generality with your predicate. You can test for equality of any field, of any type. You can test for more complex predicates for instance an either or check.
The code might look like this: class function TStudent.LinearSearch(const IsMatch: TPredicate; out Index: Integer): Boolean; var i: Integer; begin for i:= low(studentsArray) to high(studentsArray) do begin if IsMatch(studentsArrayi) then begin Index:= i; Result:= True; exit; end; end; Index:= -1; Result:= False; end; Now all you need to do is provide a suitable predicate. The definition of TPredicate, from the System.SysUtils unit, is: type TPredicate = reference to function (Arg1: T): Boolean; So you would code your method like this: class function TStudent.GetFirstName(const LastName: string): string; var Index: Integer; IsMatch: TPredicate; begin IsMatch:= function(Student: TStudent): Boolean begin Result:= Student.LastName=LastName; end; if not LinearSearch(IsMatch, Index) then begin raise. End; Result:= studentsArrayIndex.FirstName; end; And likewise for GetLastName. If your Delphi does not support anonymous methods then you won't be able to use variable capture and will have to find a more convoluted approach using of object method types. However, the basic idea will be much the same.
Recipes to master Delphi for IoT integrations, cross-platform, mobile. Class function GetHomePath: string; static; class function GetDocumentsPath: string;.
You could create a class that contains nothing but static methods. If you have to maintain some sort of state, then the state variables should be passed as var parameters. There is no way to 'properly' access static variables other than having a set of global variables in the implementation section of the class OUTSIDE the scope of the class, for example: UNIT TestUnit; INTERFACE Type TStaticClass = Class public procedure Foo(var Bar:String); static; end; IMPLEMENTATION var LastBar: String; // the last bar LastRes: string; // the last result Procedure TStaticClass.Foo(var Bar:String); begin if Bar LastBar then LastRes:= SomeCPUExpensiveProcess(Bar); LastBar:= Bar; Bar:= LastRes; end; INITIALIZATION LastBar:= '; LastRes:= SomeCPUExpensiveProcess('); END. He's asking for a feature that's not supported, so no solution is going to be perfect.
As for hints, I would normally agree and don't condone turning off hints. The hints are usually right and point out mistakes that can be serious and elusive. But look closer at this case.
Delphi Static Class Constructor
Delphi Static Class
1- The code produces NO hint in Delphi 7. So is it poor code in Delphi 2010 but not in Delphi 7? 2- The warning is 'declared but never used'. If you change the private to protected and seal the class in 2010,.Create is still hidden but the hint goes away. Does that make it better? – Jan 13 '10 at 21:12.
Comments are closed.