How to create a Guid Constant.

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
 
C

Claudio Grazioli

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
 
G

Gabriel Lozano-Morán

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
 
E

ESPNSTI

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.
 
G

Gabriel Lozano-Morán

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
 
T

Ted Miller

One correction: you state that a Guid is not a value type, which is wrong. A
Guid is in fact a value type.
 
G

Guest

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
 
T

Tom Shelton

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}");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top