Why do I get E2029 error when trying to implement a class helper for string?

强颜欢笑 提交于 2019-12-12 02:32:12

问题


We want to create a very convenient and stable conversion of data types, here string data type conversion using class helper feature in Delphi.

type
  TStringHelper = class helper for String
  public
    function AsBoolean: Boolean;
    ...
  end;

{ TStringHelper }

function TStringHelper.AsBoolean: Boolean;
begin
  Result := False;
  try
    Result := StrToBool(Self);
  except
  end;
end;

When I try to compile the above code in Delphi XE2, I get:

E2029 "declaration expected but string found"

What's the problem with my code?


回答1:


For a string type you need to use a record helper rather than a class helper.

type
  TStringHelper = record helper for string
    ....  
  end;

Note that record helpers for fundamental data types (e.g. Integer, double, string etc.) were only introduced in XE3, so if you have an older version you are out of luck.



来源:https://stackoverflow.com/questions/29847356/why-do-i-get-e2029-error-when-trying-to-implement-a-class-helper-for-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!