Answered by:
The If ... Else.. End if

Question
-
This is a sample of my program, however when "Overall" is = 18, it still throws up the MsgBox instead of skipping the if... What am i doing wrong?Private Sub Calcbut_Click(sender As Object, e As EventArgs) Handles Calcbut.Click Dim pass, merit, distinction, overall pass = Passbox.Text merit = Meritbox.Text distinction = Distinctionbox.Text overall = pass + merit + distinction If overall >= 19 Or overall <= 17 Then MsgBox("Theres only 18 Units") Passbox.Text = vbEmpty Meritbox.Text = vbEmpty Distinctionbox.Text = vbEmpty
End If Overallbox.Text = (pass * 10 * 7) + (merit * 10 * 8) + (distinction * 10 * 9) End Sub End Class
Tuesday, June 18, 2013 9:36 PM
Answers
-
Put the following at the top of your form's code:
Option Strict On Option Explicit On
You'll see some issues shown following that. Correct those then post the revised code.Please call me Frank :)
- Proposed as answer by Xiong Wei, Jin Tuesday, June 18, 2013 11:21 PM
- Marked as answer by Youen Zen Monday, July 1, 2013 6:42 AM
Tuesday, June 18, 2013 9:39 PM
All replies
-
Put the following at the top of your form's code:
Option Strict On Option Explicit On
You'll see some issues shown following that. Correct those then post the revised code.Please call me Frank :)
- Proposed as answer by Xiong Wei, Jin Tuesday, June 18, 2013 11:21 PM
- Marked as answer by Youen Zen Monday, July 1, 2013 6:42 AM
Tuesday, June 18, 2013 9:39 PM -
Your variable overall does not equal 18. Its not even an integer. You have dimmed it implicitly as an object, and later assign it the concatenation of 3 different text strings. So when it hits the If block, overall might be "0180" or "666" or something. Even if it were the string "18" it still isn't the integer 18 so it would fail the If test.
Tuesday, June 18, 2013 9:51 PM -
try this:
dim overall as integer overall = val(Passbox.Text) + val(Meritbox.Text) + val(Distinctionbox.Text)
Chuck
Tuesday, June 18, 2013 9:56 PM -
try this:
dim overall as integer overall = val(Passbox.Text) + val(Meritbox.Text) + val(Distinctionbox.Text)
Chuck
Aside from Val being a horrible method that should have been tossed out years ago - what happens if someone types in "six" rather than "6"?Please call me Frank :)
Tuesday, June 18, 2013 10:00 PM -
Ouch Frank!
Chuck
Tuesday, June 18, 2013 11:12 PM -
Were you able to get it to work?
Chuck
Wednesday, June 19, 2013 8:35 PM -
In your code replace:
Dim pass, merit, distinction, overall
with this:
Dim pass As Integer, merit As Integer, distinction As Integer, overall As Integer
It will work, it is working for me just fine :) (VS 2012)Thursday, June 20, 2013 11:18 PM -
It will work, it is working for me just fine :) (VS 2012)
It will work some of the time. It will fail if the user enters something into the textbox that is not a valid integer or, more likely, leaves a textbox blank. You should not assume that a textbox entry can be converted to an integer - it must be tested and confirmed before attempting the conversion.
Thursday, June 20, 2013 11:32 PM -
Yes I know, it will throw an exception but he can verify if the inserted text is a number (an integer) before the "if" and if it is not, another msgbox can ask the user to insert a number ... he only asked why the msgbox is fired when the sum is 18 not why it is not working when a user input text in the txtboxes ... the fine tuning is up to him ... or to other people if they want ... may be his form has a label that ask people to insert only integer numbers...Thursday, June 20, 2013 11:44 PM