Board index » delphi » How to implement class data members?

How to implement class data members?

Hi,
is there a way to define class data members in a class using Object
Pascal in Delphi?  In smalltalk it is possible, and in C++ it is
possible by declaring the data member "static".

When I tried to add the following code, the compiler protested expecting
a function or a procedure:

TDate = class(TObject)
...
  class DateName : string;
...

Also, I realized that there cannot be a class procedure with the same name
as in a (instance) procedure:

TDate = class(TObject)
...
  function NumberOfDaysBetween (Date2 : TDate) : Integer;
  class function NumberOfDaysBetween (Date2 : TDate) : Integer;

as the compiler reports a duplicate name.  Is there a way around it?

 

Re:How to implement class data members?


Quote
In article <436rpa$...@lace.Colorado.EDU> gmecc...@cc.colorado.edu writes:
>is there a way to define class data members in a class using Object
>Pascal in Delphi?  In smalltalk it is possible, and in C++ it is
>possible by declaring the data member "static".

No, there are no class data members in Delphi.  You should declare the
variables as global in the units interface section, for more or less the same
effect.

Quote
>Also, I realized that there cannot be a class procedure with the same name
>as in a (instance) procedure:
>TDate = class(TObject)
>...
>  function NumberOfDaysBetween (Date2 : TDate) : Integer;
>  class function NumberOfDaysBetween (Date2 : TDate) : Integer;
>as the compiler reports a duplicate name.  Is there a way around it?

Sure -- use a different name.  In general in Delphi you can't use the same
name for two different purposes in the same scope.

Duncan Murdoch

Other Threads