case > 1?

  • Thread starter Thread starter Donald Smith
  • Start date Start date
D

Donald Smith

Hey, does anyone know how to put this into a switch() statement?

switch(lvLogs.Items.Count)
{

case < 1:

Bitmap myBitmap_Error = new Bitmap(imageList1.Images[0]);

Icon myIcon_Error = Icon.FromHandle(myBitmap_Error.GetHicon());

}

What I want it to do is if lvLogs.Items.Count is less then one, then run my
code. But, It gives me a lot of errors, highlighting "Case < 1". ( ;
exculpated, Invalid expression <, etc.)

Does anyone know how to get around this? Thanks.
 
Donald Smith said:
Hey, does anyone know how to put this into a switch() statement?

You can't, basically. switch/case is about individual values, not
ranges. You need to use straight if statements otherwise.
 
Cases must be specific values of the property specified in the switch, not
expressions or ranges.

You probably need to use if / else along the following lines:

int intCount = lvLogs.Items.Count;

if (intCount < 1) {
// handle Count < 1
} else if (intCount == 2 {
// handle Count == 2
} else if (intCount < 5 {
// handle Count == 3 or 4
} else {
// handle all other cases
}

--Bob
 
Bob Grommes said:
Cases must be specific values of the property specified in the switch, not
expressions or ranges.

I always wished the C language allowed such a syntax. I love switch
blocks, they make the code so nice and readable.
 
None said:
[...] I love switch
blocks, they make the code so nice and readable.

Personally, I don't like the syntax of "switch" statements. I hate the
way that individual cases are delimited with "break".

Due to having to use "break", I never write more than one statement in a
case statement. If I ever need more than one, I find I have to put the
statements in a seperate method, else it looks ugly.

e.g.:

case 1:
CallAMethod();
break;

instead of:

case 1:
DoSomething();
DoSomethingElse();
DoYetAnotherThing();
break;

The second one just doesn't look right to me. It gets worse if the
statements need blank lines to logically group them.

Just my £0.02.
 
Donald said:
Hey, does anyone know how to put this into a switch() statement?

switch(lvLogs.Items.Count)
{

case < 1:

Bitmap myBitmap_Error = new Bitmap(imageList1.Images[0]);

Icon myIcon_Error = Icon.FromHandle(myBitmap_Error.GetHicon());

}

What I want it to do is if lvLogs.Items.Count is less then one, then run my
code. But, It gives me a lot of errors, highlighting "Case < 1". ( ;
exculpated, Invalid expression <, etc.)

Does anyone know how to get around this? Thanks.

You can get something similar to what you want using the Math.Sign()
method. However, I make no claims that this is in any way more readable
than the if/else alternatives:

switch (Math.Sign( lvLogs.Items.Count - 1)
{
case -1: // Count < 1
// ...
break;

case 0: // Count == 1
// ...
break;

case 1: // Count > 1
// ...
break;
}


Actually, I'd say it's less readable... it brings back memories of the
horrors of FORTRAN's computed GOTO statement.
 
C# Learner said:
None said:
[...] I love switch
blocks, they make the code so nice and readable.

Personally, I don't like the syntax of "switch" statements. I hate the
way that individual cases are delimited with "break".

Due to having to use "break", I never write more than one statement in a
case statement. If I ever need more than one, I find I have to put the
statements in a seperate method, else it looks ugly.

e.g.:

case 1:
CallAMethod();
break;

instead of:

case 1:
DoSomething();
DoSomethingElse();
DoYetAnotherThing();
break;

The second one just doesn't look right to me. It gets worse if the
statements need blank lines to logically group them.

I see what you mean, but there's a neat way of getting round it (and
the variable scoping issue that often arises from the same problem) -
use braces.

If you change the above to:

case 1:
{
DoSomething();
DoSomethingElse();
DoYetAnotherThing();
break;
}

that looks *much* cleaner to me. Maybe it's just me though...
 
Jon said:
C# Learner said:
None said:
[...] I love switch
blocks, they make the code so nice and readable.

Personally, I don't like the syntax of "switch" statements. I hate the
way that individual cases are delimited with "break".

I see what you mean, but there's a neat way of getting round it (and
the variable scoping issue that often arises from the same problem) -
use braces.

If you change the above to:

case 1:
{
DoSomething();
DoSomethingElse();
DoYetAnotherThing();
break;
}

that looks *much* cleaner to me. Maybe it's just me though...

That looks much cleaner, but I, personally, still don't like the fact
that "break" needs to be there. I guess it's just a personal issue to
me :-)
 
Jon said:
The break is indeed a bit of a shame - it's basically there to make
C/C++ programmers happier. A mistake, I feel, but not the worst thing
in the world.

I agree about it being a mistake.

I'd much rather see something like:

switch (foo)
{
case 1
{
DoSomething();
}
case 2
{
DoSomethingElse();
}
}

Actually, going slightly off-topic, I've just spent a few hours helping
someone refactor some Fortran90 code, and it just goes to remind me of
how well off we are these days with languages like C#.

:-)
 
Back
Top