How to declare const Guid ?

P

PawelF

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.

pawelf
 
D

Daniel Pratt

Hi Pawelf,

Can't be done. I suggest either keeping it as a string and converting it to a Guid value as necessary or making it a static readonly field:

public static readonly Guid GUID_1 = new Guid("CF0003D61F6E4D6AA6B17B18E5057FCD");

Regards,
Dan
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.

pawelf
 
C

Cezary Nolewajka

According to the online documentation:

The keyword const is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified. A constant declaration introduces one or more constants of a given type. The declaration takes the form:

[attributes] [modifiers] const type declarators;where:

attributes (optional)
Optional declarative information. For more information on attributes and attribute classes, see C# Attributes.
modifiers (optional)
Optional modifiers that include the new modifier and one of the four access modifiers.
type
One of the types: byte, char, short, int, long, float, double, decimal, bool, string, an enum type, or a reference type.
So, as far as I understand, there is no way to do that because Guid is not listed in the types allowed for the const keyword.

But you can get away with something like:

const string strGuid = "CF0003D6-1F6E-4D6A-A6B1-7B18E5057FCD";
Guid gd = new Guid (strGuid);

and use the strGuid where you need.

--
Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply

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.

pawelf
 
N

Nick Malik

public const Guid MyGuid = new Guid("CF0003D61F6E4D6AA6B17B18E5057FCD");

that should work.

--- Nick
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.
pawelf
 
N

Nick Malik

you can return a guid as a read only property

private Guid _myGuid = new Guid("CF0003D61F6E4D6AA6B17B18E5057FCD");
public Guid MyGuid
{
get { return _myGuid; }
}

HTH,
--- Nick
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.
pawelf
 

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