我应该使用String而不是TFilename吗?

Sim*_*mon 17 delphi string filenames delphi-7

将文件名参数传递给过程/函数时,我应该使用TFilename或String.

如果存在差异,那么它是什么,以及如果使用String?那么潜在的后果是什么?

例如

procedure TForm1.OpenFile(const AFilename : String);
begin
    //Open the file if it exists
    ...
end;
Run Code Online (Sandbox Code Playgroud)

Lin*_*nas 20

我认为TFilename应该在开发组件时使用,因为IDE可以显示它的属性编辑器(在属性检查器中单击省略号时将显示TOpenDialog).

除此之外,使用哪个基本没有区别.请记住,如果您使用,则TFilename必须添加SysUtils到您的使用条款.


klu*_*udg 10

普通代码string与TFileName类型之间唯一的实际区别在于通过引用传递参数; 以下代码

procedure GetFileName(var FileName: TFileName);
begin
  FileName:= 'abcd.abc';
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  S: string;

begin
  GetFileName(S);
end;
Run Code Online (Sandbox Code Playgroud)

不会编译错误

[DCC Error] E2033 Types of actual and formal var parameters must be identical
Run Code Online (Sandbox Code Playgroud)