Overloading a property in delphi

Hi,

I have a class as follows:


  TFileResult = class
  private
    FDate: TDateTime;
    procedure SetDate(const Value: TDateTime);
  public
    property Date:TDateTime read FDate write SetDate;
  end;


what I  want to be able to write some code as follows:


FileResult.Date := 'Apr 08 2006  15:46:23';

When I read back the Date property I want to read back a TDateTime of the above passed string.

The SetDate function would need to accept a string and be able to convert it to a TDateTime before updating it to FDate.

Is there a way to 'overload' the property in such a way to pass a different type of parameter to it?








LVL 2
WallaceAdrianAsked:
Who is Participating?
 
2266180Connect With a Mentor Commented:
no, you can't do that in delphi (nor can you overload a function with same parameters but different result type.
the this way is done is using asXxx properties:

property AsString:string;
property AsDate:TDateTime;
0
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
You can't overload a property, but you can bypass it
btw I have used "MyDate" instead of "Date" in case "Date" is a reserved word

  private
    { Private declarations }
    fMyDate: TDateTime;
  public
    { Public declarations }
    procedure MyDate(Date_: TDateTime); overload;
    procedure MyDate(Date_: string); overload;
    function MyDate: TDateTime; overload;
  end;




procedure TFileResult.MyDate(Date_: TDateTime);
begin
    fMydate := Date_;
end;

procedure TFileResult.MyDate(Date_: string);
begin
    fMydate := StrToDateTime(Date_);
end;

function TFileResult.MyDate: TDateTime;
begin
    result := fMyDate;
end;


begin
    FileResult.MyDate(Now);  // instead of saying MyDate := Now, you have to do this. Hope that's ok
    memo1.lines.add(datetimetostr(FileResult.MyDate));
    FileResult.MyDate('19/05/2006 09:51:13');
    memo1.lines.add(datetimetostr(FileResult.MyDate)); // getthe date out
end;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.