I have a certain operation in a class that is very expensive. (on the order of about 8 seconds to fully run) So, now I've decided it should probably run at the beginning of the program during an "initialization" screen. I can't find anywhere in Delphi indicating that there is such a thing as a static field however.
What I basically need to do is load a list of records and keep them alive throughout the life of the program. What is the best way to do this in Delphi?
I would do this in C# quite simply:
class Foo{
static List<...> Bar;
}
However in Delphi, I'm not seeing anything for creating a static field. All I see is the class
keyword for creating static methods
class var
to have the equivalent of static member fields, but in Delphi 7 you need to use a global variable. – Oldclass
methods andstatic
methods are similar, but slightly different.class
methods receive the class they are called on as implicit parameter, similar to how instance methods receive the instance.static
methods receive no such parameter. Old versions of Delphi only haveclass
methods, but I think newer versions also havestatic
methods for increased .net compatibility. – InaeSelf
parameter of a class method is pretty easy to come by in astatic
method. But the use ofSelf
does avoid repeating yourself. P.S. You are correct that D7 does not haveclass var
and I was about to comment to that effect in your original answer but your grace period edit cleared it up! – Oldself
parameter points to the class the method is called on, which might be a child class. And virtual class methods have no straight forward mapping at all. I sometimes miss class methods since I migrated to C#, which doesn't have them. – Inae