Just getting my toes wet with XE7, and have it in mind to use a class constructor in a situation where I want to contain some standard values as properties of the class.
Just getting my toes wet with XE7, and have it in mind to use a class constructor in a situation where I want to contain some standard values as properties of the class.
Clearly, I have failed to understand something in the workings of class constructors. Given this code, am I chasing something impossible?
type
TPrintColors = class
strict private
FExFlightShade: TColor;
FExGraphBarColor: TColor;
public
class constructor Create;
class destructor Destroy;
property ExFlightShade: TColor read FExFlightShade write FExFlightShade;
property ExGraphBarColor: TColor read FExGraphBarColor write FExGraphBarColor;
end;
implementation
const
FlightShadeColor = $00F1D9C5;
HeaderShadeColor = $00CCFFFF;
class constructor TPrintColors.Create;
begin
FExFlightShade := FlightShadeColor;
FExGraphBarColorRadio := GraphBarColorRadio;
end;
The compiler complains that the field members are not accessible. And if they are made class vars, then they cannot be fields for properties. But perhaps, they could be used through getters and setters?
The idea was to create a singleton class to contain the default values, and permit them to be customized, as may be needed.
Clearly, I have failed to understand something in the workings of class constructors. Given this code, am I chasing something impossible?
type
TPrintColors = class
strict private
FExFlightShade: TColor;
FExGraphBarColor: TColor;
public
class constructor Create;
class destructor Destroy;
property ExFlightShade: TColor read FExFlightShade write FExFlightShade;
property ExGraphBarColor: TColor read FExGraphBarColor write FExGraphBarColor;
end;
implementation
const
FlightShadeColor = $00F1D9C5;
HeaderShadeColor = $00CCFFFF;
class constructor TPrintColors.Create;
begin
FExFlightShade := FlightShadeColor;
FExGraphBarColorRadio := GraphBarColorRadio;
end;
The compiler complains that the field members are not accessible. And if they are made class vars, then they cannot be fields for properties. But perhaps, they could be used through getters and setters?
The idea was to create a singleton class to contain the default values, and permit them to be customized, as may be needed.
Martin Wienold All those are possible, but this seems the simplest, to me. Thinking of how I intend to use it, I am not seeing the risk cases you suggest.
ReplyDeleteI was just offering alternatives and ways that will make you source code testable.
ReplyDeleteHaving class vars and multiple places that may or may not modify these values ... well, it's just like having global variables.
Martin Wienold
ReplyDeleteI understand. However, this will be in a DLL, and customization of colors will be through a function call exported from the DLL; the singleton class will be accessible only inside the DLL. Even so, I will give some thought to your comments, and may yet change my mind.
I should add that the colors in question will be used in export to Excel and ReportBuilder. In consequence, the testing aspect is more than challenging, irrespective of this class.