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

后端 未结 7 753
谎友^
谎友^ 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: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;
    

提交回复
热议问题