Jump to content


Photo

Select Case not working correctly vb2008


  • Please log in to reply
8 replies to this topic

#1 timdav83

timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 01 April 2010 - 08:27 PM

I'm beginning to learn vb2008 and have a slight problem with a select case statement. The default case works as expected however, the second case doesn't. If I type in a value less then or equal to 3, nothing happens with the points label. If I type in 1 or a value 4 or greater, it works as expected.

Private Sub char_level_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles char_level.TextChanged
		Dim i As Integer
		If IsNumeric(char_level.Text) Then

			Select Case char_level.Text
				Case char_level.Text <= 3
					points.Text = 25
					Exit Sub
				Case Default
					For i = char_level.Text To 1 Step -1
						If i Mod 4 = 0 Then
							points.Text = i / 2 + 25
							Exit For
						End If
					Next i
			End Select
			
		Else
			points.Text = 0
			Exit Sub
		End If

	End Sub

The basic idea is any value that's 3 or less should display 25 in the points label. For values 4, 8, 12, etc the point value should increase by 2 and the result should display in the label.
Any help would be appreciated.

Edited by timdav83, 01 April 2010 - 08:55 PM.


#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 02 April 2010 - 08:00 PM

Case doesn't work like your typical if-statement. For the cases, you don't enter conditions, you enter values. Those values are then compared to whatever you specified in th select.

In this case, it works when you enter 1 because 'char_level.Text <= 3' evaluates to true (or integer 1), and that is the value that is applied to that case.

In order to do what you want to do, you either have to use multiple cases (easy if you are dealing strictly with integers, not floating point numbers), or use an if-else statement.

#3 timdav83

timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 03 April 2010 - 07:11 PM

I've switched back to if statements. I was assuming that it worked similarly to php's switch case where you can apply conditions, apparently it doesn't... Now for some reason, the below doesn't function properly [ reason below ]:

Module Module1

	 Public Function calculate_points_left(ByVal level As Integer)

		Dim points As Integer
		Dim i As Integer
		If IsNumeric(level) Then
			If level = 0 Then
				points = 0
			ElseIf level > 0 And level <= 3 Then
				points = 25
			ElseIf level >= 4 Then
				For i = level To 1 Step -1
					If i Mod 4 = 0 Then
						points = i / 2 + 25
					End If
				Next i
			End If

			Return points
		End If

	End Function
End Module

4 should increase the points value by 2 so total points should be 27
-- or any value between 4 and 7
8 should increase the points value by 4: total points should be 29
-- or any value between 8 and 11
12 should increase the points value by 6: total points should be 31
-- or any value between 12 - 15
and so on...

Every 4th number points increases by 2, but it's only increasing by 2 when 4 typed in the text box...

Edited by timdav83, 03 April 2010 - 07:26 PM.


#4 timdav83

timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 03 April 2010 - 08:10 PM

I needed an Exit For within the if statement, working now. Thanks for the reply rc69 by the way.

Edited by timdav83, 03 April 2010 - 08:11 PM.


#5 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 April 2010 - 11:50 PM

I was assuming that it worked similarly to php's switch case where you can apply conditions

Can you give me an example of where that works in PHP? Because it shouldn't work in PHP any more than it does in VB.

#6 timdav83

timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 05 April 2010 - 02:52 AM

I guess it's the for loop. The for loop fails in php as well. However, the below functions correctly in php.

<?php

function testIt($number, $value){

	switch($number){
		case $number < $value:
			echo 'number is less then value';
			break;
		case $number > $value;
			echo 'Number is greater then value';
			break;
		case $number >= $value;
			echo 'Number is greater then or equal to value';
			break;
	
	}
}

testIt(10, 10);
echo '<br>';
testIt(12, 10);
echo '<br>';
testIt(9, 10);


?>

Edited by timdav83, 05 April 2010 - 03:17 AM.


#7 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 05 April 2010 - 09:10 PM

That is actually a fallacy. the case statements are not actually working the way you want them to nor is PHP (though, you can't really blame PHP).

What is happening there is the same thing that is happening in VB. The condition (i.e. '$number < $value') is being evaluated first. The result is either true or false.

The reason why it happens to find the correct case (in this example), is because PHP uses loose equivalence when comparing the condition to the cases (instead of strict equivalence or the '===' operator). By that i mean that the comparison that takes place is $number == true, or $number == false. As it turns out, any non-zero number is equivalent to true, so the switch() appears to work.

Try the following example and you will see that the results you get aren't what actually want:
<?php
function testIt($number, $value){
	switch($number){
		case $number < $value:
			echo $number.' is less then '.$value;
			break;
		case $number > $value:
			echo $number.' is greater then '.$value;
			break;
		case $number >= $value:
			echo $number.' is greater then or equal to '.$value;
			break;
		default:
			echo 'nothing';
	}
}

testIt(10, 20);
echo '<br>';
testIt(10, 0);
echo '<br>';
testIt(0, 10);
echo '<br>';
testIt(0, -10);
echo '<br>';

// examples:
var_dump(10 == true);
echo '<br>';
var_dump(10 === true);
echo '<br>';
var_dump(0 == true);
?>

Ref: http://php.net/manua....comparison.php

Edited by rc69, 05 April 2010 - 09:14 PM.


#8 timdav83

timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 06 April 2010 - 12:26 AM

I see what you mean. Thanks for the info!

10 is less then 20
10 is greater then 0
0 is greater then 10
0 is less then -10
bool(true)
bool(false)
bool(false)

Edited by timdav83, 06 April 2010 - 12:27 AM.


#9 camboreadcenter

camboreadcenter

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Phnom Penh

Posted 05 May 2010 - 11:14 AM

if statement and select case statement

which one can make program perform faster?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users