none
Need Help with Nested If Statement in Visual Basic 2008

    Question

  • I need to create a form in which I will use Nested If statements to convert any given number from 0-100 to a letter grade. 

    I have a textbox named NumberGradeTextBox that the user will enter a number between 0-100. 

    I have another textbox with read only property setting named GradeTextBox which will need to display a letter grade corresponding to the number that was input into NumberGradeTextBox.

    I've spent 4 hours trying to figure it out and I am .. needless to say... very frustrated.  Here is my code:

    Private Sub IfButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles IfButton.Click
            'Displays letter grade using nested if statement

            Dim NumberGradeTextBox As Integer
            Dim GradeTextBox As String

            'Display the letter grade

            If NumberGradeTextBox <= 100 Then
                GradeTextBox = "A"
            Else
                If NumberGradeTextBox <= 89 Then
                    GradeTextBox = "B"
                Else
                    If NumberGradeTextBox <= 69 Then
                        GradeTextBox = "C"
                    Else
                        If NumberGradeTextBox <= 59 Then
                            GradeTextBox = "D"
                        Else
                            GradeTextBox = "F"
                        End If
                    End If
                End If



            End If
        End Sub

     

    Tuesday, November 23, 2010 6:10 AM

Answers

  • Way more readable:

     

            Dim NumberGradeTextBox As Integer
            Dim GradeTextBox As String

            'Display the letter grade
            Select Case True
                Case NumberGradeTextBox <= 59
                    GradeTextBox = "F"
                Case NumberGradeTextBox <= 69
                    GradeTextBox = "D"
                Case NumberGradeTextBox <= 79
                    GradeTextBox = "C"
                Case NumberGradeTextBox <= 89
                    GradeTextBox = "B"
                Case Else
                    GradeTextBox = "A"
            End Select


    Thanks,
    Corey Furman @ Facebook
    ______________________________________________________
    Please mark posts as answer or helpful when they are.
    Tuesday, November 23, 2010 1:45 PM

All replies

  • Use ElseIf statements instead of nested ifs.

    Renee

    Tuesday, November 23, 2010 6:14 AM
  • Strangly enough your code is completely in the wrong direction, Start with the F than it will probably go better.

    Now you get only an A.

    Be aware that if you are 4 hours busy, there is simply something wrong in your approach, taking one hour off helps mostly much.

    In general this kind of problems are solved with.

    http://msdn.microsoft.com/en-us/library/cy37t14y(v=VS.100).aspx

    I see also in this thread an advice to use the ElseIf, that old one, I will never use as it confuses as soon as you start nesting, than your current way is better (if you was started with the smallest value)


    Success
    Cor
    Tuesday, November 23, 2010 8:58 AM
  • This is the desired code and it works.
    Private Sub IfButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IfButton.Click
      'Displays letter grade using nested if statement
      Dim NumberGradeTextBox As Integer
      Dim GradeTextBox As String
      'Display the letter grade
      If NumberGradeTextBox <= 100 and NumberGradeTextBox >=90 Then
       GradeTextBox = "A"
      ElseIf NumberGradeTextBox <= 89 and NumberGradeTextBox >=70 Then
        GradeTextBox = "B"
      ElseIf NumberGradeTextBox <= 69 and NumberGradeTextBox >= 60 Then
         GradeTextBox = "C"
      ElseIf NumberGradeTextBox <= 59 and NumberGradeTextBox >=35 Then
          GradeTextBox = "D"
      Else
    
         GradeTextBox = "F"
    
      End If
    
     End Sub
    

    You seem to be quiet new to coding!


    Sameer Thigale "Success cannot be assured, it has to be attained"
    Tuesday, November 23, 2010 9:10 AM
  • If you put  Dim NumberGradeTextBox As Integer inside of the event handler you will get an uninitialized variable or null value . Try this out .


        
    Private Sub IfButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IfButton.Click
            
    Dim Grade As Integer
            Int32.TryParse(NumberGradeTextBox.Text, Grade)
            
    If Grade <> Nothing Then
                Select Case Grade
                    
    Case 89 To 100
                        GradeTextBox.Text = 
    "A"
                    Case 69 To 88
                        GradeTextBox.Text = 
    "B"
                    Case 59 To 68
                        GradeTextBox.Text = 
    "C"
                    Case 49 To 58
                        GradeTextBox.Text = 
    "D"
                    Case Else
                        GradeTextBox.Text = "F"
                End Select
            Else
                GradeTextBox.Text = ""
            End If
        End Sub


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261

    Tuesday, November 23, 2010 10:03 AM
  • What is wrong with my reply as it is in my idea the easiest chance and beside the select case the best logic while it is already done.

    At least it is the fastest solution (not that it matters in this case)

    I tried to avoid mining homework, but that is done by others I see now, so here the solution which is the most near the question.

     

     'Displays letter grade using nested if statement
    
     Dim NumberGradeTextBox As Integer
     Dim GradeTextBox As String
    
     'Display the letter grade
    
     If NumberGradeTextBox <= 59 Then
      GradeTextBox = "F"
     Else
      If NumberGradeTextBox <= 69 Then
      GradeTextBox = "D"
      Else
      If NumberGradeTextBox <= 79 Then
       GradeTextBox = "C"
      Else
       If NumberGradeTextBox <= 89 Then
       GradeTextBox = "B"
       Else
       GradeTextBox = "A"
       End If
      End If
      End If
    
    

     

    \

    Almost no changes in the original code of the OP.

     


    Success
    Cor
    Tuesday, November 23, 2010 1:28 PM
  • Way more readable:

     

            Dim NumberGradeTextBox As Integer
            Dim GradeTextBox As String

            'Display the letter grade
            Select Case True
                Case NumberGradeTextBox <= 59
                    GradeTextBox = "F"
                Case NumberGradeTextBox <= 69
                    GradeTextBox = "D"
                Case NumberGradeTextBox <= 79
                    GradeTextBox = "C"
                Case NumberGradeTextBox <= 89
                    GradeTextBox = "B"
                Case Else
                    GradeTextBox = "A"
            End Select


    Thanks,
    Corey Furman @ Facebook
    ______________________________________________________
    Please mark posts as answer or helpful when they are.
    Tuesday, November 23, 2010 1:45 PM