也许(很可能)这是一个愚蠢的问题,但我没有找到答案...
请检查这个假想的代码:
请检查这个假想的代码:
type
TCustomType = (Type1, Type2, Type3);
function CustomTypeToStr(CTp: TCustomType): string;
begin
Result := '';
case CTp of
Type1: Result := 'Type1';
Type2: Result := 'Type2';
Type3: Result := 'Type3';
end;
end;
function StrToCustomType(Str: string): TCustomType;
begin
Result := nil; <--- ERROR (Incompatible types: 'TCustomType' and 'Pointer')
if (Str = 'Type1') then
Result := Type1 else
if (Str = 'Type2') then
Result := Type2 else
if (Str = 'Type3') then
Result := Type3;
end;
请问,我该如何将nil / null / empty设置为这个自定义类型的变量,以便我可以检查函数结果并避免问题?
GetEnumValue
,如果返回-1,则引发异常。 - TLama