473,785 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'is' operator is giving compile error when used with switch statem

Raj
public static void HandleException (ref Exception io_exException,
bool
i_blnPropagateE xception)
{
switch (true)
{
case io_exException is ApplicationHand ledException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusi nessException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHan dledException' and
'ApplicationBus inessException' are classes inherited from
System.Applicat ionException.

Can someone please help me what is the reason and what is the way around? If
this is not possible what can be the alternative way?

May 11 '06 #1
9 1610

"Raj" <ra*******@logi cacmg.com> wrote in message
news:66******** *************** ***********@mic rosoft.com...
public static void HandleException (ref Exception io_exException,
bool
i_blnPropagateE xception)
{
switch (true)
{
case io_exException is ApplicationHand ledException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusi nessException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHan dledException' and
'ApplicationBus inessException' are classes inherited from
System.Applicat ionException.

Can someone please help me what is the reason and what is the way around?
If
this is not possible what can be the alternative way?


It means exactly what it says.

The way around it is to use if..then..else
May 11 '06 #2

Raj wrote:
public static void HandleException (ref Exception io_exException,
bool
i_blnPropagateE xception)
{
switch (true)
{
case io_exException is ApplicationHand ledException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusi nessException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHan dledException' and
'ApplicationBus inessException' are classes inherited from
System.Applicat ionException.

Can someone please help me what is the reason and what is the way around? If
this is not possible what can be the alternative way?


C#'s switch statement is not the same as VB's Select Case statement. As
the message suggests, the former requires that the cases are constants.
This increases efficiency at the cost of flexibility. Given those two
poles, we all know which C# will choose and which VB will choose :)

Something that works with non-constant cases would just be short hand
for a series of if ... else ifs. Write it out like that, and it will
work.

--
Larry Lard
Replies to group please

May 11 '06 #3
Raj,
As the error message states, you need to put a constant value on the case
statement.

"io_exExcep tion is ApplicationHand ledException" is not a constant
expression.

You may want to consider using a series of if statements instead:

if (io_exException is ApplicationHand ledException)
| {
| if (i_blnPropagate Exception)
| {
| throw io_exException;
| }
| }
else if (io_exException is ApplicationBusi nessException)
| {
| if (i_blnPropagate Exception)
| {
| throw io_exException;
| }
| }

I would consider checking blnPropagateExc eption first, something like:

if (!i_blnPropagat eException)
return;

if (io_exException is ApplicationHand ledException)
throw io_exException;
else if (io_exException is ApplicationBusi nessException)
throw io_exException;

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Raj" <ra*******@logi cacmg.com> wrote in message
news:66******** *************** ***********@mic rosoft.com...
| public static void HandleException (ref Exception io_exException,
| bool
| i_blnPropagateE xception)
| {
| switch (true)
| {
| case io_exException is ApplicationHand ledException:
| {
| if (i_blnPropagate Exception)
| {
| throw io_exException;
| }
| break;
| }
| case io_exException is ApplicationBusi nessException:
| {
| if (i_blnPropagate Exception)
| {
| throw io_exException;
| }
| break;
| }
| }
| }
|
| This function is not compiling coming with error "A constant value is
| required". In the function 'ApplicationHan dledException' and
| 'ApplicationBus inessException' are classes inherited from
| System.Applicat ionException.
|
| Can someone please help me what is the reason and what is the way around?
If
| this is not possible what can be the alternative way?
|
May 11 '06 #4
Raj
Thanks for reply Nick.

I understand that the way around is to use if..then..else. But I want to use
switch statement because I am testing the same variable against lots of
values here I have given example with 2 cases so that it looks clear and
understandable.

I am trying some other way but if someone can give me any way of doing this
I would appreciate.

Regards
Raj

"Nick Hounsome" wrote:

"Raj" <ra*******@logi cacmg.com> wrote in message
news:66******** *************** ***********@mic rosoft.com...
public static void HandleException (ref Exception io_exException,
bool
i_blnPropagateE xception)
{
switch (true)
{
case io_exException is ApplicationHand ledException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusi nessException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHan dledException' and
'ApplicationBus inessException' are classes inherited from
System.Applicat ionException.

Can someone please help me what is the reason and what is the way around?
If
this is not possible what can be the alternative way?


It means exactly what it says.

The way around it is to use if..then..else

May 11 '06 #5
There's a lot going on in this bit of code that you don't understand. My
advice to you is to first convert the statement into a simple if then else
construct. Once you understand what is going on, you can convert it back to
a switch.

For instance, the is operator returns a bool value, what you want is to use
the as operator. A throw statement transfers control out of scope, so you
shouldn't follow a throw with a break. Switch also requires a value of
integral type etc etc

--

_______________ _________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

"Raj" <ra*******@logi cacmg.com> wrote in message
news:66******** *************** ***********@mic rosoft.com...
public static void HandleException (ref Exception io_exException,
bool
i_blnPropagateE xception)
{
switch (true)
{
case io_exException is ApplicationHand ledException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusi nessException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHan dledException' and
'ApplicationBus inessException' are classes inherited from
System.Applicat ionException.

Can someone please help me what is the reason and what is the way around?
If
this is not possible what can be the alternative way?

May 11 '06 #6
I think its important that we look at why this doesn't work (as it would in
C)

we cannot even get a simpler version of this to work.

bool b = Convert.ToBoole an(args[0]);
bool b1 = Convert.ToBoole an(args[1]);
switch (true) {
case (b) :
break;
case (b1) :
break;
}
in fact we can never put a dynamic entry in as a case .. the reason for this
is that the compiler actually generates a lookup table for these items; they
are not processed as an "if" would be. We can see this by looking at the
following example.

string foo = "";
switch (foo) {
case ("test") :
break;
case ("bar") :
break;
}

which ildasms to ...

IL_0000: ldstr ""
IL_0005: stloc.0
IL_0006: ldstr "test"
IL_000b: ldstr "bar"
IL_0010: leave.s IL_0012
IL_0012: ldloc.0
IL_0013: dup
IL_0014: stloc.1
IL_0015: brfalse.s IL_0034
IL_0017: ldloc.1
IL_0018: call string [mscorlib]System.String:: IsInterned(stri ng)
IL_001d: stloc.1
IL_001e: ldloc.1
IL_001f: ldstr "test"
IL_0024: beq.s IL_0030
IL_0026: ldloc.1
IL_0027: ldstr "bar"
IL_002c: beq.s IL_0032
IL_002e: br.s IL_0034
IL_0030: br.s IL_0034
IL_0032: br.s IL_0034
IL_0034: ret

http://benjaminm.net/PermaLink.aspx?...c-5c345d0e13f5
explains more ..

Cheers,

Greg Young
MVP - C#
"Raj" <ra*******@logi cacmg.com> wrote in message
news:66******** *************** ***********@mic rosoft.com...
public static void HandleException (ref Exception io_exException,
bool
i_blnPropagateE xception)
{
switch (true)
{
case io_exException is ApplicationHand ledException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusi nessException:
{
if (i_blnPropagate Exception)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHan dledException' and
'ApplicationBus inessException' are classes inherited from
System.Applicat ionException.

Can someone please help me what is the reason and what is the way around?
If
this is not possible what can be the alternative way?

May 11 '06 #7

"Raj" <ra*******@logi cacmg.com> wrote in message
news:7E******** *************** ***********@mic rosoft.com...
Thanks for reply Nick.

I understand that the way around is to use if..then..else. But I want to
use
switch statement because I am testing the same variable against lots of
values here I have given example with 2 cases so that it looks clear and
understandable.
- You don't want or need "ref" in the parameter list.

- It looks like you can cut things down quite a bit by checking
i_blnPropagateE xception first.

- You should never need a lot of cases because you should have a nice
exception hierarchy and be catching base classes.

I would write it as something like:
if( i_blnPropagateE xception )
{
throw io_exException;
}
else if( io_exException is ApplicationHand ledException )
{
}
else if( io_exEception is ApplicationBusi nessException )
{
}
else
{
throw ....
}

I would suggest that ApplicationHand ledException is fundamentally wrong. The
whole point of exceptions is that the thrower does not know how to handle
the situation - It is wrong for it to presume to know whether or not the
caller can or will handle it. If you just use ApplicationBase exception
instead then you can derive ApplicationBusi nessException from that and
eliminate one case.
I am trying some other way but if someone can give me any way of doing
this
I would appreciate.
switch does what it does and only VB programmers seem to have a problem with
it.
[It is actually much more flexible than C/C++ switch because you can have
strings.]

Regards
Raj

"Nick Hounsome" wrote:

"Raj" <ra*******@logi cacmg.com> wrote in message
news:66******** *************** ***********@mic rosoft.com...
> public static void HandleException (ref Exception io_exException,
> bool
> i_blnPropagateE xception)
> {
> switch (true)
> {
> case io_exException is ApplicationHand ledException:
> {
> if (i_blnPropagate Exception)
> {
> throw io_exException;
> }
> break;
> }
> case io_exException is ApplicationBusi nessException:
> {
> if (i_blnPropagate Exception)
> {
> throw io_exException;
> }
> break;
> }
> }
> }
>
> This function is not compiling coming with error "A constant value is
> required". In the function 'ApplicationHan dledException' and
> 'ApplicationBus inessException' are classes inherited from
> System.Applicat ionException.
>
> Can someone please help me what is the reason and what is the way
> around?
> If
> this is not possible what can be the alternative way?


It means exactly what it says.

The way around it is to use if..then..else

May 11 '06 #8
Raj wrote:
Thanks for reply Nick.

I understand that the way around is to use if..then..else. But I want to use
switch statement because I am testing the same variable against lots of
values here I have given example with 2 cases so that it looks clear and
understandable.

I am trying some other way but if someone can give me any way of doing this
I would appreciate.


If you really want to achieve switch/case look-and-feel, how about
throwing it once more and then catch again, but this time specifically?

public static void HandleException (ref Exception io_exException,
bool i_blnPropagateE xception)
{
try
{
throw io_Exception;
}
catch (ApplicationHan dledException e)
{
if (i_blnPropagate Exception)
throw io_exException;
}
catch (ApplicationBus inessException e)
{
if (i_blnPropagate Exception)
throw io_exException;
}
}
May 11 '06 #9
Raj <ra*******@logi cacmg.com> wrote:
This function is not compiling coming with error "A constant value is
required".
The switch statement requires constants in the case clauses.
Can someone please help me what is the reason and what is the way around? If
this is not possible what can be the alternative way?


Use a set of nested if statements instead.

-- Barry
May 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2340
by: Canonical Latin | last post by:
#include<iostream> int main() { char buff; std::cin.getline(buff,3); std::cin.getline(buff,3); std::cout << buff << endl; } Run at command prompt and input 1234567 what do you get as output?
4
2227
by: Dan | last post by:
Hi, I would just like to know if the istream operator takes only one parammeter(object) at a time (like z) ? istream operator>>(istream& in, Shape &z) Cause I keep getting error concerning the amount my operator has for bother cin , cout operator<< and >> thanks Dan
193
9649
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
17
3502
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using , operator there one operand will be there for ()? And Unary operators like !, +, -, ~, ... are said to be having associativity right to left which means that we can write, (but not allowed) 1. 2! 2. !2
134
8046
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've increasingly been using scripting languages (especially Python and Bourne shell) which offer the same speed and yet are far more simple and safe to use. I can no longer understand why anyone would willingly use C to program anything but the lowest...
24
2966
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been reading several old posts/threads on the subject, and they never end with a conclusion (people keep correcting each other and disagreeing).
1
970
by: Raj | last post by:
public static void HandleException(ref Exception io_exException, bool i_blnPropagateException) { switch (true) { case io_exException is ApplicationHandledException: { if (i_blnPropagateException) {
8
3328
by: ilikenwf | last post by:
I get 27 errors when I try to compile, none of which make sense to me, since the code looks good. I have my header file included, and all of my function definitions are in the class contained in the header. You can see an assignment sheet here . I am not looking for someone to do it for me, but rather someone to point out the error causing code...This program is supposed to do what is described in the sheet linked above. And now for the...
8
1863
by: Joshua Moore | last post by:
/* Hi, I was hoping someone could help me with this problem. I did my work and worked my way through the usual compiler messages, but I have run against some problem I can't identify. The compiler error message is unintelligable -- to me anyway. Anyway: Here is the code, maybe someone can tell me what is wrong with it. */ //buysome.cpp //Joshua Moore //the part responsible for the buying part of the farm pos simulation program
0
9485
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
10161
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
10098
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
9958
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...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
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
6743
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
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.