【问题标题】:Scoping issue with Set typesSet 类型的范围问题
【发布时间】:2016-11-19 02:26:42
【问题描述】:

我有 3 个单位这是我的主要单位....

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

Uses Unit2;

{$R *.dfm}


procedure TForm1.FormShow(Sender: TObject);
begin
  Panel1.Visible := oiNone in form2.Settings.OptVars;
  Panel2.Visible := oiHint in form2.Settings.OptVars;
  Panel3.Visible := oiStat in form2.Settings.OptVars;
end;

end.

这是第二个单元 - 由第一个单元使用。 单元Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Unit3;

type
  TForm2 = class(TForm)
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Settings: TSettings;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
  Settings := TSettings.Create;
  Checkbox1.Checked := oiNone in Settings.OptVars;
  CheckBox2.Checked := oiHint in Settings.OptVars;
  CheckBox3.Checked := oiStat in Settings.OptVars;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  Settings.Free;
end;

end.

这是第二个单元使用的单元,包含其他两个单元都使用的 Set 类型。

unit Unit3;

interface

Uses
  winAPI.windows,system.classes;

Type
  TOptions = Set Of (oiNone, oiHint, oiStat);

  TSettings = class

  private
    fMyOption:    TOptions;
  public
    Constructor Create;
    Destructor Destroy;
    property OptVars: TOptions read fMyOption write fMyOption;
  end;


implementation

constructor TSettings.Create;
begin
  fMyOption := [oiNone, oiHint, oiStat];
end;

destructor TSettings.Destroy;
begin

end;

end.

在第二个单元中,oiNone、oiHint、oiStat 项是可访问的并且在范围内。

在第一个单元中,尽管可以访问 TOption 数据类型的 Settings.OpVars,但项目 oiNone、oiHint 和 oiStat 不可访问且在范围内。

我想不出更好的方式来描述我的问题。如果您将这些放入项目中,您应该会看到问题。

【问题讨论】:

  • 不,这不在一起。当您编写此代码时,您以某种方式删除了有用的信息。再试一次。明确minimal reproducible example
  • 您的两个脱离上下文的代码 sn-ps 是无用的,尤其是第二个(根本没有提及或引用tOptions)。请发布 minimal reproducible example 来证明您遇到的问题。 (顺便说一句,类型通常以大写的T 为前缀,以清楚地将它们标识为类型,以将它们与变量名区分开来。)
  • 希望我更新的问题更有意义
  • 你是说当我使用我在第三个单元中展示给你的代码时,它不起作用。让我们再试一次。请张贴minimal reproducible example 来说明问题。请注意该文本中的 MinimalComplete 这两个词,您现在已被要求提供 3 次。
  • 您的代码非常令人失望。远非最小。

标签: delphi


【解决方案1】:

更新

在最新的编辑之后,很明显我的第一个预感是正确的。您没有使用声明类型的单元。名称已更改,但在当前编辑中,您的问题是主机不使用Unit3


您需要引用声明集合类型的单元及其枚举类型。将UnusedItemsReg 添加到第三单元的uses 子句中。

如果你这样做了,那么我能想象的唯一其他解释是你编译时启用了作用域枚举。但我现在正抓着稻草。

我绝对建议您将枚举类型声明为本身的命名类型。当然,如果您使用范围枚举,那么您将需要这样做。但迟早你会想要那种类型。

type
  TOption = (oiNone, oiHint, oiStat); 
  TOptions = set of TOption;

如果您确实使用范围枚举,那么您将像这样引用 then:

TOption.oiNone

【讨论】:

    【解决方案2】:

    在主单元的 (a) uses 子句中需要 Unit3。否则它看不到类型 TOptions。这是德尔福对可见性的要求。它不使用您似乎正在寻找的隐式引用。

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 2015-01-30
      • 1970-01-01
      • 2018-11-10
      • 2011-04-18
      • 2020-04-26
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多