-
Jun 22nd, 2012, 04:58 PM
#1
Thread Starter
Junior Member
None of my If statements are executing.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Process1.Click
Dim Number As String
Dim Display As String
Number = Val(txtNumber.Text)
Display = lblDisplay.Text
If IsNumeric(Number) = True Then
If Number < 1 Or Number > 100 Then
If Number > 100 Then
Display = " The Number You entered is too high"
ElseIf Number < 1 Then
Display = " The Number You entered is too low"
ElseIf Number >= 1 And Number <= 100 Then
Process1.Enabled = False
End If
Else
Display = " The number you entered is not between 1 and 100"
End If
Else
Display = " You did not enter a numerical value "
End If
End Sub
End Class
My statements are not executing and I am not getting any of my Display messages to come out on my Form when I Run the program.
-
Jun 22nd, 2012, 05:20 PM
#2
Re: None of my If statements are executing.
It's because
Code:
Number = Val(txtNumber.Text)
would never be numeric, it will allways be a string because you've declared it as such here
Code:
Dim Number As String
also, why not use a numericalupdown for this process?
Edit - If you insist on using a textbox, then add this to your code:
Code:
Private Sub txtNumber_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub
That away, you'll never have any characters other than numbers(and the backspace) in that textbox. Or take a look at JMc's numerical only textbox here.
Last edited by dday9; Jun 22nd, 2012 at 05:26 PM.
-
Jun 22nd, 2012, 05:27 PM
#3
Re: None of my If statements are executing.
Originally Posted by dday9
It's because
Code:
Number = Val(txtNumber.Text)
would never be numeric, it will allways be a string because you've declared it as such here
Code:
Dim Number As String
also, why not use a numericalupdown for this process?
Actually that is not the case. IsNumeric() can be used to see if a string contains a numeric value which will always be true when it has been assigned using a Val() statement.
Code:
Dim x As String = "34"
MsgBox(IsNumeric(x))
Would be true
of course all the if tests are treating Number as though it is a numeric variable when it is in fact a string and even though this would usually work it is wrong to do it that way.
As for why it is not working best to set a break point at the first if statement then single step to see what is happening.
-
Jun 22nd, 2012, 05:28 PM
#4
Re: None of my If statements are executing.
Also, turn Option Strict ON for the project in the Project|Properties menu (on the Compile tab). That will probably result in dozens of errors, as you are showing lots of confusion about data types. Option Strict will prevent you from making incorrect implicit conversions, which will result in better habits and faster code.
My usual boring signature: Nothing
-
Jun 22nd, 2012, 05:29 PM
#5
Thread Starter
Junior Member
Re: None of my If statements are executing.
Should I also change the string to an integer? I'm actually using a text box because the assignment requires me use if then statements with a text box and label. Will adding that code make the program display correctly?
-
Jun 22nd, 2012, 05:30 PM
#6
Re: None of my If statements are executing.
This would be better but still would not work as expected
Code:
Dim Number As Integer
Dim Display As String
Display = lblDisplay.Text
If IsNumeric(txtNumber.Text) = True Then
Number = Val(txtNumber.Text)
If Number < 1 Or Number > 100 Then
If Number > 100 Then
Display = " The Number You entered is too high"
ElseIf Number < 1 Then
Display = " The Number You entered is too low"
ElseIf Number >= 1 And Number <= 100 Then
Process1.Enabled = False
End If
Else
Display = " The number you entered is not between 1 and 100"
End If
Else
Display = " You did not enter a numerical value "
End If
Last edited by DataMiser; Jun 22nd, 2012 at 05:38 PM.
-
Jun 22nd, 2012, 05:37 PM
#7
Re: None of my If statements are executing.
The real problem with the code is actually here
Code:
If Number < 1 Or Number > 100 Then
If Number > 100 Then
Display = " The Number You entered is too high"
ElseIf Number < 1 Then
Display = " The Number You entered is too low"
ElseIf Number >= 1 And Number <= 100 Then
Process1.Enabled = False
End If
Else
Display = " The number you entered is not between 1 and 100"
End If
The message saying that the number is not between 1 and 100 will be displayed when the number is between one and 100
For example 50 is not <1 nor is it > 100 so the code jumps straight to the red else section and gives a false message
Edit : The other problem is that Display is not being used after it is assigned so it probably does not show any message
Would need to add
Code:
lblDisplay.Text=Display
at the bottom if it is not already there.
Edit 2: The code could be rewritten and simplified to something like this
Code:
Dim Number As Integer
If IsNumeric(txtNumber.Text) = True Then
Number = Val(txtNumber.Text)
If Number > 100 Then
lblDisplay.Text = " The Number You entered is too high"
ElseIf Number < 1 Then
lblDisplay.Text = " The Number You entered is too low"
Else
lblDisplay.Text = ""
Process1.Enabled = False
End If
Else
lblDisplay.Text = " You did not enter a numerical value "
End If
and it should actually work but is still using some old methods leftover from pre .net vb versions.
Last edited by DataMiser; Jun 22nd, 2012 at 07:22 PM.
-
Jun 23rd, 2012, 03:11 PM
#8
Re: None of my If statements are executing.
Originally Posted by Shaggy Hiker
Also, turn Option Strict ON for the project in the Project|Properties menu (on the Compile tab). That will probably result in dozens of errors, as you are showing lots of confusion about data types. Option Strict will prevent you from making incorrect implicit conversions, which will result in better habits and faster code.
Take this man's advice....It would keep you from making such errors in the future.
-
Oct 12th, 2012, 12:42 PM
#9
New Member
Re: None of my If statements are executing.
Hello everyone, I'm working on a class assignment that has a user input numerical values for a geometry calculator and then it displays the answers in console mode. I wanted to expand on the program by using if then statements. I found the examples above very helpful, but I am still not able to construct what I need to work. And Yes, this is my first time in the forum and working with VB so take it easy on the green horn. Here is what I have put together, any assistance would be greatly appreciated. I was unable to find a way to cut and paste the code the same way that was used else where..Sorry
Thanks
Sub Main()
Dim Number As Integer
Dim Display As String
Console.WriteLine("Please input a number")
Dim input = Console.ReadLine()
If input(Number) = True Then
Number = Val(Number)
If Number < 1 Or Number > 100 Then
If Number > 100 Then
Console.WriteLine(" The Number You entered is too high")
ElseIf Number < 1 Then
Console.WriteLine(" The Number You entered is too low")
ElseIf Number >= 1 And Number <= 100 Then
Process1.Enabled = False
End If
Else
Display = " The number you entered is not between 1 and 100"
End If
Else
Display = " You did not enter a numerical value "
End If
End Sub[HIGHLIGHT]Sub Main()
-
Oct 12th, 2012, 02:21 PM
#10
Re: None of my If statements are executing.
Code:
If Number < 1 Or Number > 100 Then
'Code here will only execute when number is not between 1 and 100
If Number > 100 Then
Console.WriteLine (" The Number You entered is too high")
ElseIf Number < 1 Then
Console.WriteLine (" The Number You entered is too low")
ElseIf Number >= 1 And Number <= 100 Then ' this will never be true due to the top if statement
Process1.Enabled = False 'This will never execute
End If
Else
'This will execute only when the number is between 1 and 100
Display = " The number you entered is not between 1 and 100"
End If
-
Oct 12th, 2012, 02:29 PM
#11
Re: None of my If statements are executing.
We aren't terribly harsh on most people. However, you really should have started your own thread for this. Don't be bashful about it, as it's all about eyeballs around here. The more people that see your post, the more chances that you will get useful information. For that reason, a new thread if almost always the right option, even if you started the original thread. As long as the question is a new one, a new thread is the right way to go to maximize the likelihood of suitable answers. Somebody might jump on me for that, though, as I don't know that I said it quite right, but I'd err on the side of too many threads rather than too few, until somebody objects.
As for the question, there are lots of things to say:
1) This line should result in an error at compile time, since it isn't valid code:
If input(Number) = True Then
You might have wanted IsNumeric, but leave that aside for the moment.
2) In the next line you use Val(). I kind of like that function, but it is deprecated VB6 code. You don't have to check whether the input was numeric if you use Val, since Val will ALWAYS return a number. However, Val is also risky, because the way it turns a string into a number can be really surprising to people. It stops parsing as soon as it hits a character that isn't a number, so:
Val("123") = 123
Val("12.3") = 12.3
Val("ABC") = 0
Val("1,234") = 1
Val("5A") = 5
Val("A5") = 0
The first three are probably expected, as is the last one, but the fourth and fifth might surprise you. The proper way to convert a string into a number would be to use Integer.TryParse (since you want an integer). That would look like this:
Code:
If Integer.TryParse(input,Number) Then
'It was a number, and is now in Number.
Else
'It was not a number.
End If
3) See what Datamiser wrote, as you have the same mistake about the If Number < 0 Or Number > 100 Then
(though, in both cases, you should be using OrElse rather than Or).
My usual boring signature: Nothing
-
Oct 12th, 2012, 03:28 PM
#12
Re: None of my If statements are executing.
Duplicate post - http://www.vbforums.com/showthread.p...f-Then-command ...
Also, how about we skip IsNumeric for Integer.Tryparse and skip the use of Val as well while we're at it? nvm - I see shaggy covered that.
-tg
-
Oct 12th, 2012, 04:23 PM
#13
New Member
Re: None of my If statements are executing.
Shaggy, you're right about starting a new thread for this question I actually did do that after I posted on here..noobie mistake. techgnome already posted my new thread below. I'm still working on the project and trying to take everything in. I'll give it another shot before asking more questions. Thanks
DB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|