469,133 Members | 1,209 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

How to create a Guid Constant.

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
Nov 17 '05 #1
7 19037
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
Nov 17 '05 #2
On Mon, 25 Apr 2005 11:38:31 -0500, ESPNSTI wrote:
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
--
Claudio Grazioli
http://www.grazioli.ch
http://www.grazioli.ch/HommingbergerGepardenforelle/
Nov 17 '05 #3
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.
Nov 17 '05 #4
> 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
Nov 17 '05 #5
One correction: you state that a Guid is not a value type, which is wrong. A
Guid is in fact a value type.

"Claudio Grazioli" <ne********@gmx-ist-cool.de> wrote in message
news:dg*****************************@40tude.net...
On Mon, 25 Apr 2005 11:38:31 -0500, ESPNSTI wrote:
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
--
Claudio Grazioli
http://www.grazioli.ch
http://www.grazioli.ch/HommingbergerGepardenforelle/

Nov 17 '05 #6
KH
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

"Claudio Grazioli" wrote:
On Mon, 25 Apr 2005 11:38:31 -0500, ESPNSTI wrote:
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
--
Claudio Grazioli
http://www.grazioli.ch
http://www.grazioli.ch/HommingbergerGepardenforelle/

Nov 17 '05 #7
On 2005-04-25, ESPNSTI <ES*********@Hotmail.com> wrote:
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}");

--
Tom Shelton [MVP]
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brent Horine | last post: by
2 posts views Thread by Tomas Larsson | last post: by
4 posts views Thread by PawelF | last post: by
24 posts views Thread by M O J O | last post: by
9 posts views Thread by twang090 | last post: by
7 posts views Thread by =?Utf-8?B?SlA=?= | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.