-
November 19th, 2012, 10:33 PM
#1
Problem with Nested If..Else statement
Hello Folks:
I am new at VB.net so this question may be very easy. I have been at this for 2 days with no results. What I am trying to achieve through my code is the following. First, check if the value is numeric, if it is then convert to decimal, if not display message box stating so. Next, I want to check if the value of decWeight is greater than 0, if it is not then display a message box advising. If it is greater than zero but greater than 500 and radKilograms is checked, then give me a message box that the value is invalid, and do not perform any calculations. If the value is between 0 and 499, then perform the calculation.
Now that part works just fine, however, my next part of the code states that if radPounds is checked and decWeight is greater than 225, then display do the same as above. Here is where my problem is. If I enter the Else statatement for both the radPounds and radKilograms, I get the message boxes, but still performs the calculations. If I remove the Else statement from one of the rad then it works fine but just for that section of the code.
I can't figure out for my llife what I am doing wrong or why it only works half way.
Code:
'Declare variables
Dim decWeight As Decimal
Dim decWeightConverter As Decimal = 2.2D
Dim decTotalWeightInPounds As Decimal
Dim decTotalWeightInKilograms As Decimal
Dim strMessageBox As String = "Enter A Valid Number"
If IsNumeric(txtWeight.Text) Then
decWeight = Convert.ToDecimal(txtWeight.Text)
If decWeight > 0 Then
If radKilograms.Checked And decWeight >= 500 Then
MsgBox(strMessageBox, , "Input Error")
Else
decTotalWeightInKilograms = decWeight / decWeightConverter
lblResults.Text = decTotalWeightInKilograms.ToString("F1") & " Kilograms"
End If
If radPounds.Checked And decWeight >= 225 Then
MsgBox(strMessageBox, , "Input Error")
Else
decTotalWeightInPounds = decWeight * decWeightConverter
lblResults.Text = decTotalWeightInPounds.ToString("F1") & " Pounds"
End If
Else
MsgBox("You entered " & decWeight.ToString() & ". Please Enter a Positive Number", , "Input Error")
txtWeight.Text = ""
txtWeight.Focus()
End If
Else
MsgBox("Enter the Weight in Numbers", , "Input Error")
txtWeight.Text = ""
txtWeight.Focus()
End If
End Sub
-
November 20th, 2012, 04:36 AM
#2
Re: Problem with Nested If..Else statement
-
November 21st, 2012, 06:53 PM
#3
Re: Problem with Nested If..Else statement
Thank you, it was my logical operators. I had them in the wrong place
-
November 21st, 2012, 07:23 PM
#4
Re: Problem with Nested If..Else statement
nevermind... I did not see that it had be resolved
Always use [code][/code] tags when posting code.
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
|