473,395 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Raj
public static void HandleException(ref Exception io_exException,
bool
i_blnPropagateException)
{
switch (true)
{
case io_exException is ApplicationHandledException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusinessException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHandledException' and
'ApplicationBusinessException' are classes inherited from
System.ApplicationException.

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 1591

"Raj" <ra*******@logicacmg.com> wrote in message
news:66**********************************@microsof t.com...
public static void HandleException(ref Exception io_exException,
bool
i_blnPropagateException)
{
switch (true)
{
case io_exException is ApplicationHandledException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusinessException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHandledException' and
'ApplicationBusinessException' are classes inherited from
System.ApplicationException.

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_blnPropagateException)
{
switch (true)
{
case io_exException is ApplicationHandledException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusinessException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHandledException' and
'ApplicationBusinessException' are classes inherited from
System.ApplicationException.

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_exException is ApplicationHandledException" is not a constant
expression.

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

if (io_exException is ApplicationHandledException)
| {
| if (i_blnPropagateException)
| {
| throw io_exException;
| }
| }
else if (io_exException is ApplicationBusinessException)
| {
| if (i_blnPropagateException)
| {
| throw io_exException;
| }
| }

I would consider checking blnPropagateException first, something like:

if (!i_blnPropagateException)
return;

if (io_exException is ApplicationHandledException)
throw io_exException;
else if (io_exException is ApplicationBusinessException)
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*******@logicacmg.com> wrote in message
news:66**********************************@microsof t.com...
| public static void HandleException(ref Exception io_exException,
| bool
| i_blnPropagateException)
| {
| switch (true)
| {
| case io_exException is ApplicationHandledException:
| {
| if (i_blnPropagateException)
| {
| throw io_exException;
| }
| break;
| }
| case io_exException is ApplicationBusinessException:
| {
| if (i_blnPropagateException)
| {
| throw io_exException;
| }
| break;
| }
| }
| }
|
| This function is not compiling coming with error "A constant value is
| required". In the function 'ApplicationHandledException' and
| 'ApplicationBusinessException' are classes inherited from
| System.ApplicationException.
|
| 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*******@logicacmg.com> wrote in message
news:66**********************************@microsof t.com...
public static void HandleException(ref Exception io_exException,
bool
i_blnPropagateException)
{
switch (true)
{
case io_exException is ApplicationHandledException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusinessException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHandledException' and
'ApplicationBusinessException' are classes inherited from
System.ApplicationException.

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*******@logicacmg.com> wrote in message
news:66**********************************@microsof t.com...
public static void HandleException(ref Exception io_exException,
bool
i_blnPropagateException)
{
switch (true)
{
case io_exException is ApplicationHandledException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusinessException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHandledException' and
'ApplicationBusinessException' are classes inherited from
System.ApplicationException.

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.ToBoolean(args[0]);
bool b1 = Convert.ToBoolean(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(string)
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*******@logicacmg.com> wrote in message
news:66**********************************@microsof t.com...
public static void HandleException(ref Exception io_exException,
bool
i_blnPropagateException)
{
switch (true)
{
case io_exException is ApplicationHandledException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
case io_exException is ApplicationBusinessException:
{
if (i_blnPropagateException)
{
throw io_exException;
}
break;
}
}
}

This function is not compiling coming with error "A constant value is
required". In the function 'ApplicationHandledException' and
'ApplicationBusinessException' are classes inherited from
System.ApplicationException.

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*******@logicacmg.com> wrote in message
news:7E**********************************@microsof t.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_blnPropagateException 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_blnPropagateException )
{
throw io_exException;
}
else if( io_exException is ApplicationHandledException )
{
}
else if( io_exEception is ApplicationBusinessException )
{
}
else
{
throw ....
}

I would suggest that ApplicationHandledException 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 ApplicationBusinessException 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*******@logicacmg.com> wrote in message
news:66**********************************@microsof t.com...
> public static void HandleException(ref Exception io_exException,
> bool
> i_blnPropagateException)
> {
> switch (true)
> {
> case io_exException is ApplicationHandledException:
> {
> if (i_blnPropagateException)
> {
> throw io_exException;
> }
> break;
> }
> case io_exException is ApplicationBusinessException:
> {
> if (i_blnPropagateException)
> {
> throw io_exException;
> }
> break;
> }
> }
> }
>
> This function is not compiling coming with error "A constant value is
> required". In the function 'ApplicationHandledException' and
> 'ApplicationBusinessException' are classes inherited from
> System.ApplicationException.
>
> 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_blnPropagateException)
{
try
{
throw io_Exception;
}
catch (ApplicationHandledException e)
{
if (i_blnPropagateException)
throw io_exException;
}
catch (ApplicationBusinessException e)
{
if (i_blnPropagateException)
throw io_exException;
}
}
May 11 '06 #9
Raj <ra*******@logicacmg.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
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
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...
193
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...
17
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 ,...
134
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...
24
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...
1
by: Raj | last post by:
public static void HandleException(ref Exception io_exException, bool i_blnPropagateException) { switch (true) { case io_exException is ApplicationHandledException: { if...
8
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...
8
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...

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.