小编Z.B*_*.B.的帖子

嵌套通用记录

我在尝试定义嵌套通用记录时遇到了一个奇怪的编译器错误.

嵌套适用于类和接口,但不能以某种方式记录.

type
  TRec<T> = record
    Value: T;
  end;

  TCls = class
  public
    Rec: TRec<TRec<Integer>>;
  end;
Run Code Online (Sandbox Code Playgroud)

这不是在Delphi Berlin 10.1.2上编译的,在东京10.2.3也没有运气.这是语言或编译器问题的限制吗?

错误消息是:

[dcc32错误] Project1.dpr(22):E2564未定义类型'TRec <T>'

我只想要一次嵌套Spring.Nullable<>类型,但是没有用.之后我用一个简单的通用记录快速复制了它.

delphi generics delphi-10.1-berlin delphi-10.2-tokyo

10
推荐指数
1
解决办法
257
查看次数

TSaveDialog文件扩展名和[ofOverwritePromt]问题

关于TSaveDialog和Delphi的 [ofOverwritePromt]的概念已经存在一个简单的问题,它会在保存对话框中覆盖现有文件.

所以我的问题/场景如下:

  • 我有一个 TSaveDialog
  • 我设置了[ofOverwritePromt]Options
  • 我将过滤器设置为"PDF(*.pdf)|*.pdf"
  • 过滤器索引设置为1

所以现在我执行程序并调用对话框.如果我选择WITH MOUSE或KEYBOARD(没有输入)的文件存在,则保存对话框要求我用消息覆盖:

保存对话框

但是,如果我执行相同的操作但输入文件名如'Test'而未指定扩展名,则保存对话框不会确认覆盖.我知道实际上它返回另一个文件名"C:\ Users\xxx\Desktop\Test"而不是"C:\ Users\xxx\Desktop\Test.pdf".如果对话框要求您保存文件,但是您需要键入扩展名,这有点不太好.所以通常我会像这样处理它:

repeat
  { Ask for the file if not silent }
  if not dlgSave.Execute then
    Exit;

  { Read the filename from the save dialog }
  LTempFile := dlgSave.FileName;
  if not SameText(ExtractFileExt(LTempFile), '.pdf') then
    begin
      { Add the extension }
      LTempFile := LTempFile + '.pdf';

      { As we bypassed the overwrite check in dialog do it now }
      if FileExists(LTempFile) then
        if MsgWarn(Format('%s …
Run Code Online (Sandbox Code Playgroud)

delphi tsavedialog

8
推荐指数
1
解决办法
1824
查看次数

记录构造函数和字段初始化错误

Delphi 10.2.3中的以下代码:

uses
  System.SysUtils;

type
  TRec = record
  strict private
    FName: String;
    FValue: Integer;
  public
    property Name: String read FName;
    property Value: Integer read FValue;

    constructor Create(const AName: String);
    function WithValue(const AValue: Integer): TRec;
  end;

constructor TRec.Create(const AName: String);
begin
  FName := AName;
end;

function TRec.WithValue(const AValue: Integer): TRec;
begin
  Result := Self;
  Result.FValue := AValue;
end;

procedure Main;
var
  x: TRec;
begin
  x := TRec.Create('First').WithValue(666);
  x := TRec.Create('Second');
  Writeln('In stack: ', x.Value);
end;

var
  x: TRec;
begin
  x := …
Run Code Online (Sandbox Code Playgroud)

delphi constructor record

4
推荐指数
1
解决办法
113
查看次数