Pau*_*ema -4 delphi properties
我通常使用 C# 编程,但也使用旧语言 Delphi。据我所知,在 Delphi 中,属性使用大量代码,如下所示:
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;
Run Code Online (Sandbox Code Playgroud)
在 C# 中,我可以像这样编写上面的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
Delphi 中是否有像 C# 中那样更短的表示法?