By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

Using static readonly Guid in switch/case

P: n/a
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(){}

public static readonly Guid Read = new
Guid("AAAAAAAA-AAAA-AAAA-0001-AAAAAAAAAAAA");

}

Then I want to use this constant in a switch case statement like below:

switch(someGuid)

{

case Activities.Read:

//do something

break;

}

I thought one possible way was to convert both guids to strings instead,
since the case statmement accepts strings, but this will generate an error
"A constant value is expected".

Why is this, and how whould it be done instead? I don't want to declare the
constants as strings because I want them to have the correct type.

/Tomas Larsson
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Switch statements are probably bound at compile-time and therefore your
example would not work because Activities.Read is not known until the
program is run.

Just a guess.

You might have to resolve to if...then statements.

"Tomas Larsson" <so*****@microsoft.com> wrote in message
news:Ov**************@TK2MSFTNGP09.phx.gbl...
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(){}

public static readonly Guid Read = new
Guid("AAAAAAAA-AAAA-AAAA-0001-AAAAAAAAAAAA");

}

Then I want to use this constant in a switch case statement like below:

switch(someGuid)

{

case Activities.Read:

//do something

break;

}

I thought one possible way was to convert both guids to strings instead,
since the case statmement accepts strings, but this will generate an error
"A constant value is expected".

Why is this, and how whould it be done instead? I don't want to declare the constants as strings because I want them to have the correct type.

/Tomas Larsson

Nov 15 '05 #2

P: n/a
Tomas Larsson wrote:
Why is this, and how whould it be done instead? I don't want to
declare the constants as strings because I want them to have the
correct type.


You can't compare anything other than primitive types in a switch. A
Guid is not a primitive type. Try changing your code to something like
Activities.Read.ToString();

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.