Parallel Array- VB Coding If/Then/Else Statement


Here I have the code for this visual basic exercise I am working on in the visual basic 2008 book which I AM NOT USING FOR ACADEMIC (SCHOOL) USE. I need help coding the btnDisplay event. I was wondering how to use an If/Then/Else Statement and a parallel array. I need to store the minimum points I have listed below into a 5-element, one-dimensional integer array named intPoints. Also, for the grades that I have listed below I need a 5-element, one-dimensional string array named strGrades. The arrays should be parallel arrays. The procedure should search the intPoints array for for the number of points entered by the user, and then display the corresponding grade from the strGrades array.

Thanks for all the help you experts do!!

Minimum Points          Maximum Points          Grade
0                                                299                          F
300                                       349                                D
350                                       399                          C
400                                        449                          B
450                                       500                         A

Here is the code I have so far

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
    End Sub

    Private Sub txtPoints_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.Enter
    End Sub

    Private Sub txtPoints_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPoints.KeyPress
        ' accepts only numbers and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtPoints_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.TextChanged
        lblGrade.Text = String.Empty
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

    End Sub

End Class

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi anault24;

Seeming that you have clarified your question in this post try this.

Public Class Form1

    Dim minPoints() As Integer = {0, 300, 350, 400, 450}
    Dim maxPoints() As Integer = {299, 349, 399, 449, 500}
    Dim strGrades() As String = {"F", "D", "C", "B", "A"}
    '' Your other code here
    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

      Dim pointsEntered As Integer = 0
      If Integer.TryParse(txtPoints.Text, pointsEntered) Then
          If Not (pointsEntered >= minPoints(0) AndAlso pointsEntered <= maxPoints(maxPoints.Length - 1)) Then
              lblGrade.Text = "Outside the given limits"
          End If
          For index As Integer = 0 To minPoints.Length - 1
              If pointsEntered >= minPoints(index) AndAlso pointsEntered <= maxPoints(index) Then
                  lblGrade.Text = strGrades(index)
              End If
      End If
    End Sub

End Class

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Did that help you out @anault24?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.