Using static readonly Guid in switch/case

T

Tomas Larsson

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
 
P

Peter Rilling

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.
 
F

Frank Oquendo

Tomas said:
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)
 

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