switch case with true

E

Eric S.

Why doesn't something like this work? I get "a constant value is
expected" on all 3 case statements.

private DataGrid CurrentDataGrid(DataGrid firstDg, DataGrid secondDg)
{
try
{
switch (true)
{
case firstDg.Items.Count != 0 & secondDg.Items.Count != 0:
throw new Exception();
break;
case firstDg.Items.Count != 0:
return firstDg;
case secondDg.Items.Count != 0:
return secondDg;
default:
return null;
}
}
catch
{
return null;
}
}
 
C

Carlos J. Quintero [.NET MVP]

The values in C# case statements must be constants (at compile-time) and you
are using variables such as firstDg.Items.Count. Case statements of VB.NET
(or VB6) do not have such restriction, just in case you come from that
background.

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Eric said:
Why doesn't something like this work? I get "a constant value is
expected" on all 3 case statements.
<snip>

Let's say the example was compilable. If two of those compiled to true,
what then ? I know that that's not a valid case in your example, but the
compiler needs to guarantee that it only takes one branch, and adding
static expression analysis to this case would make it really hard for
the compiler to guarantee that.

The syntax for switch requires you to use constants for the cases, so
that's what you have to do. In your case I would go with a couple of
if-statements.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

and you may getting also a warning, saying something about a constant value
being used in a switch

the case(s) labels should be constants, it cannot be an expression that need
to be evaluated in runtime, its values need to be know at compile time as
the compiler generate the jumps instructions.

In your case you can use several if cnostructions.


Cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi

I would like some comments about the use a while/break in a multiple case
escenario as described in the parent post.

Now thinking about what I said before regarding switch and constants
expression, it should be common the situation where you have multiple
exclusives cases ( as in a switch) but the decision of which one to execute
is not constant ( so you cannot use switch ) thinking in this I saw a couple
of solutions

1- multiple if/elseif it may get cumbersome to understand the code,
especially if the editor does not format it correctly.

2-Use an auxiliary method , where each possible case ends with a return ,
this is a better idea but it can get complex if any case use local variables
, in this escenario it will be necessary to pass several parameters and it
may get difficult to understand.

3- And this is the escenario I would like to hear opinion about , using a
while ( true ) as a way to avoid using the "else if" constructions and
without moving the code to another method
if would looks like

while( true )
{
if ( case 1 )
{
.....
break;
}

if ( case 2 )
{
.....
break;
}

if ( case 3 )
{
.....
break;
}



break;
}



The code looks clean, there are not a chained if string and do what is
expected to execute at least one case

Has anybody use this construction before?

What are your comments about it?

Cheers,
 
C

carl.manaster

Hi, Ignacio,

I don't like it, personally, because I don't find it very
intention-revealing. "while," to me, carries an implication of
repetition. I like your option 2 much better.

Also, for mutually exclusive cases, "else if" is only a matter of
efficiency, not necessity - you can simply put the "if" statements in
sequence. Sometimes I find this cleaner. I don't much like "else if":

<http://onlysyntax.blogspot.com/2003/04/conditionals-else-if-to-me-one-clue.html>
Peace,
--Carl
 
C

carl.manaster

Hi, Ignacio,

I don't like it, personally, because I don't find it very
intention-revealing. "while," to me, carries an implication of
repetition. I like your option 2 much better.

Also, for mutually exclusive cases, "else if" is only a matter of
efficiency, not necessity - you can simply put the "if" statements in
sequence. Sometimes I find this cleaner. I don't much like "else if":

<http://onlysyntax.blogspot.com/2003/04/conditionals-else-if-to-me-one-clue.html>
Peace,
--Carl
 
N

Nick Malik [Microsoft]

I've not used "while(true)" because a mistake in coding could lead to an
infinite loop.
On the other hand, I have used your "method 2" where I created seperate
private methods that effectively did the same thing, only they used the
"return" to leave the method and return to the caller. You are right that
this isn't always applicable if you have a very large number of local
variables.

Another common approach is to set a "done flag"

bool done = false;
if (condition1 and not done)
{
// do stuff
done = true;
}
if (condition2 and ! done)
{
// do stuff
done = true;
}


However, none of these are particularly object oriented. The OO answer to
this is the Chain of Responsibility pattern.
http://www.dofactory.com/Patterns/PatternChain.aspx

Hope this helps,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I don't like it, personally, because I don't find it very
intention-revealing. "while," to me, carries an implication of
repetition. I like your option 2 much better.

I think the same, it may be confusing but I found it interesting anyway. I
had never thought about that until I was writing the first answer.
Option 2 is workable when there are not lots of locals variables involved,
it should be easier with VS.net 05 and refactoring though.



Cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Wont it better if the done flag is the first conditional?
if you put it in the last it will evaluate the others anyway and this may
not be very efficient.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
 

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