switch doesn't like bool test?

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

Help seems to only show switch statements using equality rather than
comparison
I wanted to compare to a renage of values...
Point position = Mouse.GetPosition(Listview1 );

switch (position.X)

case < 150:

//column 1

case < 450:

//column2

case else:

//column3

do i have to use if then else costruct instead of switch?

thanks

mark
 
mp said:
Help seems to only show switch statements using equality rather than
comparison
I wanted to compare to a renage of values...
Point position = Mouse.GetPosition(Listview1 );

switch (position.X)
case < 150:
//column 1
case < 450:
//column2
case else:
//column3

do i have to use if then else costruct instead of switch?

Yes, you do. Visual Basic supports ranges in its Select statement, but C#
(like C and C++) only supports individual values.
 
I think you are correct that you will need to use the if else construct, but
that isn't really that hard (I am assuming you are using C#, VB.NET does
have what you want in it's Select statement). Here is an example from the
documentation:

if (Condition_1)
{
// Statement_1;
}
else if (Condition_2)
{
// Statement_2;
}
else if (Condition_3)
{
// Statement_3;
}
else
{
// Statement_n;
}

It may not be as convenient as the switch statement, but it's still pretty
simple and the code is very organized, so you don't need to worry about
confusing yourself with complicated messy code. Hopefully this helps.
 
mp said:
Help seems to only show switch statements using equality rather than
comparison
I wanted to compare to a renage of values...
Point position = Mouse.GetPosition(Listview1 );

switch (position.X)

case < 150:

//column 1

case < 450:

//column2

case else:

//column3

do i have to use if then else costruct instead of switch?

thanks

mark

In addition to the other suggestions, you may consider creating a
function returning the column number, and switch on that returned value.
You would still have the if-then-else, construct, but 1) the routine
is likely needed elsewhere, and 2) it keeps this section of code cleaner
for the next person to maintain it.
 
Family Tree Mike said:
mp said:
Help seems to only show switch statements using equality rather than
comparison
I wanted to compare to a renage of values...
[...]\

In addition to the other suggestions, you may consider creating a function
returning the column number, and switch on that returned value. You would
still have the if-then-else, construct, but 1) the routine is likely
needed elsewhere, and 2) it keeps this section of code cleaner for the
next person to maintain it.

Thanks Mike,
what's weird is i only see your response...were there others? in fact i
don't even see my op...
(using oe in xp- view all messages...your reply is the only one i see...)
i'll try google to see if i can find them
mark
 
Back
Top