473,385 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 21800
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
2
by: Brent Horine | last post by:
Still a newbie to C#. How do I declare a Guid as a constant? public const Guid GUID_BLUETOOTH_HCI_EVENT = new Guid(0x850302a, 0xb344, 0x4fda, 0x9b, 0xe9, 0x90, 0x57, 0x6b, 0x8d, 0x46, 0xf0); ...
2
by: Tomas Larsson | last post by:
Hi. I have some problems when using a static readonly declaration of a guid in a switch/case statement. I'll give you an example. public sealed class Activites { private Activites(){}
4
by: PawelF | last post by:
I need to replace string with Guid ready to use: public const string GUID_1 = "CF0003D61F6E4D6AA6B17B18E5057FCD"; like public const Guid ... Is there any chance to do this.
2
by: Nadav | last post by:
Hi, I am using the System.Type.GUID to identify similar types on different computers, one computer send the GUID through a socket while the other resolve the GUID and identify the type, when...
2
by: csharper | last post by:
Why does Guid.GetHashcode() returns 0 when instantiated with Guid.Empty ?
24
by: M O J O | last post by:
Hi, Instead of doing this.... Public Class Form1 Public Shared Sub CreateAndShow() Dim f As New Form1 f.Show() End Sub
9
by: twang090 | last post by:
Trying to create a 12 digit "guid", but have no idea on how to, anyone please have any thought? Thanks in advance.
7
by: =?Utf-8?B?SlA=?= | last post by:
I need to design a WS that will after authenicating the user, create a cookie on the users PC that made the request. All the code I keep finding is how to get a WS to read a cookie, I need it to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.