473,666 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using static readonly Guid in switch/case

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
2 15659
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*****@micros oft.com> wrote in message
news:Ov******** ******@TK2MSFTN GP09.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3958
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it will not set the value of the control and will leave the checked status of the checkbox to false when a user selects a new date. this works fine when using the control on a win2k machine but if we use it on a win XP box and call
1
1740
by: Greif | last post by:
I am trying to call a static method within a sealed class. However, the wrong method keeps getting called instead. In a seperate class I am calling the following code: LogHelper.Log("Sample Exception", ex, LogLevel.ERROR); Here is the weird thing, the public static void Log(string message, LogLevel level) method is always called instead of the public static void Log(string message, Exception e, LogLevel level) method. I don't know why....
0
2389
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add D 'Delete
3
3053
by: i | last post by:
Hi, I'm working with an array of nodes, numbering roughly in the thousands. Each node has at least one, but up to four, references to another node - North, South, East, or West. I'm trying to get my program to take these nodes and plot them on a graph, represented by a two-dimensional array. Right now I'm having some trouble with the recursive method I've set up, which does not seem to be efficient enough to get the job done. I
14
3272
by: Steve Teeples | last post by:
I don't understand why I cannot use a property to modify data within a struct. Can someone tell me why I get the error "Cannot modify the return value of "myData.TheData" because it is not a variable. Here is what breaks. struct Data { public Data(){} private int someData;
1
2023
by: colinjack | last post by:
Hi All, I've been using the original (non-event based) asynchronous design pattern using delegates. It works fine but performance wise I know using the ThreadPool is supposed to be far better, so I wondered if anyone had any examples of the best way to do this. I've obviously had a look myself but haven't gotten very far and although I've implemented a version myself I'm not sure its the best that can be done.
5
2997
by: Sek | last post by:
hi folks, i have a bunch of strings used in my code in many places. these strings reside inside a instantiable class. so, i want to replace these with constant/static variable to control the occurences. i have been looking at both static and const variables as the options, but couldn't decide on one.
3
8271
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
0
1064
by: sloan | last post by:
Is there a way to loop over the static variables of a struct? public struct FunFoods {
0
8445
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
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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
8781
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
8551
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,...
1
6198
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
5664
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();...
1
2771
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.