[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2016
  • Last Modified:

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?








0
WallaceAdrian
Asked:
WallaceAdrian
2 Solutions
 
2266180Commented:
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
 
TheRealLokiSenior 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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now