views:

759

answers:

6

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.

A: 

You can create an array of IInterface.

Gamecat
I actually need the GUIDs though, not instances of IInterface
David
+3  A: 

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}');
Sascha Hennen
Ah, that works. I wanted to pull the GUIDs directly from the interface type though, as keeping them in sync manually is a bit dangerous.
David
A: 

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)

Ulrich Gerhardt
What's that in English?
Mason Wheeler
Oops. I'll edit my post.
Ulrich Gerhardt
That's interesting, is there a __uuidof equivalent in Delphi? Just doing Const MyGUID : TGUID = IInterface; produced an error for me.
David
I have no idea. I tried some casts like TGUID(IInterface) but nothing worked. Maybe this is the time where some Delphi compiler engineer should chime in? ;-)
Ulrich Gerhardt
+7  A: 

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);
TOndrej
I like this the best, it's a bit more manual work than I'd hoped but it looks like it doesn't leave any traps for any subsequent programmers, which is the most important thing. Thanks!
David
+1  A: 

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;
David
I guess in that case it would be easier to just use "var MyArray...". :-)
Ulrich Gerhardt
Oh yeah! Good point :-)
David
+1  A: 

Another idea: The following compiles:

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

Blah([IInterface, IDispatch]);

Maybe you can use this approach.

Ulrich Gerhardt
That's quite a nice approach. It happens to not really suit my case, but I can imagine often being able to rework things into this method
David