I wrote this code in Delphi XE which assign the GUID of the interface from GUID const. the code compile ok, but I want to know if this a valid declaration?
const
IID_IFoo: TGUID = '{00000000-6666-6666-6666-666666666666}';
type
IFoo = interface(IDispatch)
[IID_IFoo]
//properties
//methods
end;
You can do this, but the question is why would you want to ?
If you wish to refer to the GUID of an interface rather than the name of the interface, then as long as that interface has an associated IID (GUID) then you can use the interface name where a TGUID is expected:
This is a less “noisy” declaration of the interface and avoids the possibility that you might declare/reference an IID constant which isn’t actually associated with the interface you think it is (or which hasn’t been associated with that IID at all).
Using the interface itself as both interface and IID, rather than having a separate const declaration, removes the potential for these mistakes.
When using separate constant declarations for IID’s – if you absolutely have to – you can protected against one of these problems by never-the-less using interfaces where IID’s are expected. But this arguably makes things worse in the case where the wrong IID has been used for a particular interface: