Home > OS >  Is there a shorter notation for properties in Delphi?
Is there a shorter notation for properties in Delphi?

Time:09-07

I'm usually programming in C# but also in the old language Delphi. In Delphi, for as far as I know, a property uses a lot of code like so:

private
  FOPPORTUNITY_NR   : string;

  procedure SetOPPORTUNITY_NR(const aOPPORTUNITY_NR: string);
  function  GetOPPORTUNITY_NR: string;

public
  property  OPPORTUNITY_NR: string read GetOPPORTUNITY_NR write SetOPPORTUNITY_NR;

implementation

procedure TTypeName.SetOPPORTUNITY_NR(const aOPPORTUNITY_NR: string);
begin
  if (SpecialStrUtils_13.IsBlankStr(aOPPORTUNITY_NR)) then
    raise Exception.CreateFmt('OPPORTUNITY_NR cannot be empty');

  FOPPORTUNITY_NR := aOPPORTUNITY_NR;
end;

function TTypeName.GetOPPORTUNITY_NR: string;
begin
  if (SpecialStrUtils_13.IsBlankStr(FOPPORTUNITY_NR)) then
    raise Exception.CreateFmt('OPPORTUNITY NUMBER not set');

  Result := FOPPORTUNITY_NR;
end;

In C# I can write the code above like so:

private string? _opportunityNr = null;

public string OpportunityNr 
{ 
    get => _opportunityNr ?? throw new Exception("Opportunitynr not set");
    set => _opportunityNr = String.IsNullOrEmpty(value) ? throw new Exception("Opportunitynr can not be empty") : value;
}

Is there a shorter notation in Delphi like in C#?

CodePudding user response:

Is there a shorter notation in Delphi like in C#?

No.

CodePudding user response:

You don't need to use methods:

property  OPPORTUNITY_NR: string read FOPPORTUNITY_NR write FOPPORTUNITY_NR;
  • Related