473,659 Members | 3,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21950
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********@g mx-ist-cool.de> wrote in message
news:dg******** *************** ******@40tude.n et...
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*********@Ho tmail.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
2742
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 = server.createobject("scriptlet.typelib").guid guid=Left(guid,instr(guid,"}")) genguid=guid
2
6663
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); gives me the following error: "The expression being assigned to "...GUID_BLUETOOTH_HCI_EVENT" must be constant" String and array initializers give me trouble also.
2
15659
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
13892
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
3908
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 working on a single computer everything works fine, BUT, when working on two different computers the GUID extracted from one type is different from the GUID extracted from the same type on the other computer. why is that happening? shouldn't a guid...
2
4231
by: csharper | last post by:
Why does Guid.GetHashcode() returns 0 when instantiated with Guid.Empty ?
24
2812
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
6834
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
1988
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 create one. The below code doesnt add the cookie to my cache. However if I place this in an non asmx page its fine Any help you offer is apperiated public string CreateToken(string User, string App, string County) {
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.