delphi XE用TPath类获取路径IDE解析不了类方法编译也要报错

delphi XE用TPath类获取路径IDE解析不了类方法编译也要报错

问题:今天有同学在群里问到,为啥TPath类获取路径不了类方法编译也要报错:

是因为我们通常用FMX开发Multi-device Application,在创建项目时,IDE自动为你生成了对FMX.Objects单元的引用,而后,你为了获取路径,加入了System.IOUtils单元。

在这两个单元中,均包含有TPath,而FMX.Objects的成员类T是从持久类TPersistent(uses System.Classes),IDE在设计时会调取其资源文件获取成员类TPath = class(TCustomPath);而System.IOUtils也包含一个记录类型的TPath = record。

所以,IDE解析不出来,在设计时,你到底是想引用哪个名称空间的所有者,是FMX.Objects.TPath还是System.IOUtils.TPath:

  TPersistent = class(TObject)
  private
    procedure AssignError(Source: TPersistent);
  protected
    procedure AssignTo(Dest: TPersistent); virtual;
    procedure DefineProperties(Filer: TFiler); virtual;
    function GetOwner: TPersistent; dynamic;
  public
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); virtual;
    function GetNamePath: string; dynamic;
  end;

解决方法:在引用属性和类时,之前其名称空间:System.IOUtils.TPath。

  LPath:=System.IOUtils.TPath.Combine(System.IOUtils.TPath.GetDocumentsPath,'hhg.db');

这样就没有问题啦:

附相关文章:1、delphi持久类TPersistent派生的组件TComponent的序列化和反序列化:https://blog.csdn.net/pulledup/article/details/105768280

喜欢的话,就在下面点个赞、收藏就好了,方便看下次的分享:

猜你喜欢

转载自blog.csdn.net/pulledup/article/details/105767353