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;
}
} 9 8830
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
"Eric S." <mo*****@hotmail.com> escribió en el mensaje
news:11**********************@c13g2000cwb.googlegr oups.com... 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; } }
Eric S. wrote: 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.
--
Lasse Vågsæther Karlsen http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
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,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Eric S." <mo*****@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com... 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; } }
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,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Eric S." <mo*****@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com... 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; } }
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
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
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.
--
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OQ****************@TK2MSFTNGP14.phx.gbl... 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,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
"Eric S." <mo*****@hotmail.com> wrote in message news:11**********************@c13g2000cwb.googlegr oups.com... 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; } }
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,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
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
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:ce********************@comcast.com... 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. -- "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:OQ****************@TK2MSFTNGP14.phx.gbl... 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,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
"Eric S." <mo*****@hotmail.com> wrote in message news:11**********************@c13g2000cwb.googlegr oups.com... > 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; > } > } >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Myster Ious |
last post by:
Polymorphism replaces switch statements, making the code more
compact/readable/maintainable/OO whatever, fine!
What I understand, that needs to be done at the programming level, is
this:
a...
|
by: Andy |
last post by:
Can someone tell me if the following Switch...Case construct is valid?
I'm wanting to check for multiple values in the Case statement without
explicitly listing each values.
So for example, will...
|
by: YitzHandel |
last post by:
The following is an extract from my program, which I seperated out and
compiled as its own program.
The problem is that as soon as a case tests true, all the cases below
it execute as well. So...
|
by: Chih-Hsu Yen |
last post by:
I encountered a strange problem about switch-case statement.
switch(cmd)
{
case 1: statements; break;
case 2: statements; break;
... ....
case 11: S1;
S2;
S3;
statements;
|
by: Benny Raymond |
last post by:
I'm confused as to how fallthrough is limited in switch. For example
the following works:
string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
| |
by: Fei Liu |
last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am
working with scientific data files in netCDF format. One of the
properties of netCDF data is that the actual type of data is only...
|
by: sam_cit |
last post by:
Hi Everyone,
I read somewhere that there are some compile time operations behind
switch-case, which is why it can work for cases which evaluates to an
integer or character and not strings and...
|
by: asit |
last post by:
please modify to get the correct output.(switch case is compulsory)
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character : ");
ch=getch();
switch(ch)
|
by: Satya |
last post by:
Hi everyone,
This is the first time iam posting excuse me if iam making any
mistake. My question is iam using a switch case statement in which i
have around 100 case statements to compare. so...
|
by: Annalyzer |
last post by:
My form looks like this:
<form action="handle_event.php" method="POST" enctype="multipart/form-data">
<table id="event_edit" border="0">
<tr>
<td>
...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
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...
| |