How do you initialise a const array of TGUID from Interface type data, in Delphi?

后端 未结 7 729
谎友^
谎友^ 2020-12-18 00:43

I want to initialise an array like this -

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

But it results in -

[DCC Err         


        
相关标签:
7条回答
  • 2020-12-18 01:24

    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);
    
    0 讨论(0)
  • 2020-12-18 01:27

    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}');
    
    0 讨论(0)
  • 2020-12-18 01:30

    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;
    
    0 讨论(0)
  • 2020-12-18 01:34

    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)

    0 讨论(0)
  • 2020-12-18 01:35

    Another idea: The following compiles:

    procedure Blah(const MyArray: array of TGUID);
    begin
      //...
    end;
    
    Blah([IInterface, IDispatch]);
    

    Maybe you can use this approach.

    0 讨论(0)
  • 2020-12-18 01:38

    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;
    
    0 讨论(0)
提交回复
热议问题