How to create a Guid Constant.

  • Thread starter Thread starter ESPNSTI
  • Start date Start date
E

ESPNSTI

Hi,

I'd like to create a Guid constant and the following doesn't work (Cannot
implicitly convert type 'string' to 'System.Guid') :

public const Guid MyGuid = "{ccae2079-2ebc-4200-879d-866fc82e6afa}";

Ideas?

Thanks,
Erik
 
Hi,

I'd like to create a Guid constant and the following doesn't work (Cannot
implicitly convert type 'string' to 'System.Guid') :

public const Guid MyGuid = "{ccae2079-2ebc-4200-879d-866fc82e6afa}";

Ideas?

Thanks,
Erik

Only value types can be declared as constants. A Guid is not a value type. But
you can declare it as a static readonly.

public static readonly Guid MyGuid = new
Guid("{ccae2079-2ebc-4200-879d-866fc82e6afa}");

hth
 
Should be a constant

public const string MyGuid = "{ccae2079-2ebc-4200-879d-866fc82e6afa}";

or just declare a public variable

public Guid MyGuid = new Guid("{ccae2079-2ebc-4200-879d-866fc82e6afa}");

Gabriel Lozano-Morán
 
Thanks.

Well, that kinda bites :(.
I was hoping to use it in a custom attribute (which doesn't like the public
static readonly), I'll just have to use the Guids in string format.
 
Only value types can be declared as constants. A Guid is not a value type.
But
you can declare it as a static readonly.

And also two reference types the null type and string

Gabriel Lozano-Morán
 
One correction: you state that a Guid is not a value type, which is wrong. A
Guid is in fact a value type.
 
Guid *is* a value type; from .NET SDK:

[Serializable]
public struct Guid : IFormattable, IComparable

I think you mean only build-in types can be const. The SDK states only these
types can be const:

byte, char, short, int, long, float, double, decimal, bool, string

KH
 
Hi,

I'd like to create a Guid constant and the following doesn't work (Cannot
implicitly convert type 'string' to 'System.Guid') :

public const Guid MyGuid = "{ccae2079-2ebc-4200-879d-866fc82e6afa}";

Ideas?

Thanks,
Erik

public static readonly Guid MyGuid = new Guid ("{ccae2079-2ebc-4200-879d-866fc82e6afa}");
 
Back
Top