在 Delphi 中,如何从接口类型数据初始化 TGUID 常量数组?

发布于 2024-07-15 22:59:29 字数 494 浏览 6 评论 0原文

我想初始化一个像这样的数组 -

Const MyArray : Array[0..0] Of TGUID = (IInterface);

但它会导致 -

[DCC Error] Test.pas(10): E2010 Incompatible types: 'TGUID' and 'string'

所以为了看看会发生什么,我尝试了这个 -

Const MyArray : Array[0..0] Of String = (IInterface);

结果是这样!

[DCC Error] Test.pas(10): E2010 Incompatible types: 'string' and 'TGUID'

多么奇怪! 当然,IInterface 是其中之一,但它似乎顽固地转变成错误的类型。

I want to initialise an array like this -

Const MyArray : Array[0..0] Of TGUID = (IInterface);

But it results in -

[DCC Error] Test.pas(10): E2010 Incompatible types: 'TGUID' and 'string'

So to see what would happen I tried this -

Const MyArray : Array[0..0] Of String = (IInterface);

Which results in this!

[DCC Error] Test.pas(10): E2010 Incompatible types: 'string' and 'TGUID'

How strange! Surely IInterface is one or the other, but it seems to stubbornly transform into the wrong type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

撧情箌佬 2024-07-22 22:59:29

您可以从接口声明中提取 GUID 并将它们声明为(字符串)常量。 然后,您可以在接口声明和数组常量声明中使用这些常量。 编译器接受有效的 GUID 字符串,其中需要 TGUID。 无效的字符串会导致 E2204“不正确的 GUID 语法”编译错误。

const
  MyGuid1 = '{99BDAB12-B1B6-41B0-9BF1-2C1DB3D8EC70}';
  MyGuid2 = '{8C7CD303-8D81-469B-99ED-E1F163E9036F}';

type
  IMyInterface1 = interface
    [MyGuid1]
  end;

  IMyInterface2 = interface
    [MyGuid2]
  end;

const
  MyArray: array[0..1] of TGUID = (MyGuid1, MyGuid2);

You can pull the GUIDs from the interface declarations and declare them as (string) constants. You can then use these constants in your interface declarations and your array constant declarations. The compiler accepts valid GUID strings where TGUID is expected. Invalid strings result in E2204 "Improper GUID syntax" compile error.

const
  MyGuid1 = '{99BDAB12-B1B6-41B0-9BF1-2C1DB3D8EC70}';
  MyGuid2 = '{8C7CD303-8D81-469B-99ED-E1F163E9036F}';

type
  IMyInterface1 = interface
    [MyGuid1]
  end;

  IMyInterface2 = interface
    [MyGuid2]
  end;

const
  MyArray: array[0..1] of TGUID = (MyGuid1, MyGuid2);
沦落红尘 2024-07-22 22:59:29

如果您使用 const 数组,则必须使用 const 值对其进行设置,如下所示:

const GuidArray: array[0..0] of TGuid=
  ('{84DBCC66-72AA-4806-AE28-B55FC5B83FC8}');

If you use a const array you have to set it up with const values like this:

const GuidArray: array[0..0] of TGuid=
  ('{84DBCC66-72AA-4806-AE28-B55FC5B83FC8}');
爱已欠费 2024-07-22 22:59:29

您可以编写一个函数来返回 GUID 数组。 (我将这种技术用于恒定日期值。)

  • 它不是“真正的”常量,但应该可以在您通常使用该常量的任何地方使用。
  • 但它也不能使用“可分配类型常量”选项进行修改。 不允许作弊:)
  • 所以它比在初始化部分中设置全局有一个微小的优势。
  • 此外,与将接口使用的 GUID 移至其自己的常量相比,手动工作更少。

您可以选择返回动态或固定大小的数组。

选项 1

type
  TGUIDArray = array of TGUID;

function GetMyInterfaces: TGUIDArray;
begin
  SetLength(Result, 2);
  Result[0] := IMyInterface1;
  Result[1] := IMyInterface2;
end;

选项 2

type
  TGUIDArray = array[0..1] of TGUID;

function GetMyInterfaces: TGUIDArray;
begin
  Result[0] := IMyInterface1;
  Result[1] := IMyInterface2;
end;

You could write a function to return your array of GUIDs. (I use this technique for constant date values.)

  • It's not "truly" a constant, but should be usable wherever you'd ordinarily use the constant.
  • But it also cannot be modified using the "assignable typed constants" option. Cheating not allowed :)
  • So it has a tiny advantage over setting a global in the initialization section.
  • Also, it's less manual work than moving the GUIDs used by the interfaces into their own constants.

You have the choice of returning a dynamic or fixed size array.

Option 1

type
  TGUIDArray = array of TGUID;

function GetMyInterfaces: TGUIDArray;
begin
  SetLength(Result, 2);
  Result[0] := IMyInterface1;
  Result[1] := IMyInterface2;
end;

Option 2

type
  TGUIDArray = array[0..1] of TGUID;

function GetMyInterfaces: TGUIDArray;
begin
  Result[0] := IMyInterface1;
  Result[1] := IMyInterface2;
end;
蓦然回首 2024-07-22 22:59:29

我刚刚在 C++Builder 中尝试过:

const TGUID g = __uuidof(IInterface);
const TGUID MyArray[] = {__uuidof(IInterface)};

我怀疑显式关键字 __uuidof 可能会避免您遇到的问题,但它只是用非常相似的东西替换它。虽然第一行工作正常,但第二行产生:(

[C++ Fehler] Unit1.cpp(9): E2034 Konvertierung von 'const _GUID' nach 'unsigned long' nicht möglich

英语:[ C++ 错误] Unit1.cpp(9): E2034 从“const _GUID”转换为“unsigned long”不可能)

I just tried in C++Builder:

const TGUID g = __uuidof(IInterface);
const TGUID MyArray[] = {__uuidof(IInterface)};

I suspected that the explicit keyword __uuidof might avoid the problem you have, but it merely replaces it with something very similar.While the first line works fine, the second one yields:

[C++ Fehler] Unit1.cpp(9): E2034 Konvertierung von 'const _GUID' nach 'unsigned long' nicht möglich

(in English: [C++ error] Unit1.cpp(9): E2034 Conversion from 'const _GUID' to 'unsigned long' not possible)

红玫瑰 2024-07-22 22:59:29

另一个想法:以下编译:

procedure Blah(const MyArray: array of TGUID);
begin
  //...
end;

Blah([IInterface, IDispatch]);

也许你可以使用这种方法。

Another idea: The following compiles:

procedure Blah(const MyArray: array of TGUID);
begin
  //...
end;

Blah([IInterface, IDispatch]);

Maybe you can use this approach.

弄潮 2024-07-22 22:59:29

您可以创建一个 IInterface 数组。

You can create an array of IInterface.

梦在夏天 2024-07-22 22:59:29

这是我发现的一种方法,利用了传统上 const 在 delphi 中并不是真正的 const 的事实。 需要编译器开关才能返回此行为(在 D2007 中)

{$J+}
Const MyArray : Array[0..0] Of TGUID = (());
{$J-}

在初始化部分 -

MyArray[0] := IInterface;

Here's a way I discovered using the fact that traditionally, consts are not really const in delphi. Requires a compiler switch to return to this behaviour (In D2007)

{$J+}
Const MyArray : Array[0..0] Of TGUID = (());
{$J-}

In initialization section -

MyArray[0] := IInterface;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文