Problem with Nested If..Else statement
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Problem with Nested If..Else statement

  1. #1
    Join Date
    Nov 2012
    Posts
    3

    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

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,074

    Re: Problem with Nested If..Else statement


  3. #3
    Join Date
    Nov 2012
    Posts
    3

    Re: Problem with Nested If..Else statement

    Thank you, it was my logical operators. I had them in the wrong place

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,781

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center