[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Parallel Array

I know what I need to do Just not sure of how to start it off, The program is supposed to have points entered and it should display the grade Letter corresponding to the points. I have the key press limitations and the clearing done. I just need a push in the right direction with linking the array. Here is what I have so far.


Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    Private Sub Display_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'assign array values
        Dim Points() As Integer = {450, 400, 350, 300, 0}
        Dim Grade() As String = {"A", "B", "C", "D", "F"}


    End Sub

    Private Sub txtPoints_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPoints.KeyPress
        ' allows the text box to accept 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

End Class

Open in new window

0
Mkelliny
Asked:
Mkelliny
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
When the "btnDisplay" button is clicked, you need to convert the string value in your "txtPoints" textbox to an integer so it can be compared to your "Points" array.
0
 
MkellinyAuthor Commented:
I am not sure I understand what you are saying, can you clarify?
0
 
brutaldevCommented:
Firstly you need to move you variables outside the scope of your click method so that they can be accessed by other methods/event handlers.

Convert your text to an Integer: System.Convert.ToInt32(txtPoints.Text)
Do a range check to determine your Grade

You code will look something like this:
 
Public Class frmMain

    'assign array values
    Dim Points() As Integer = {450, 400, 350, 300, 0}
    Dim Grade() As String = {"A", "B", "C", "D", "F"}

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    Private Sub Display_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' put something here...


    End Sub

    Private Sub txtPoints_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPoints.KeyPress
        ' allows the text box to accept 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
        Dim val As Integer = System.Convert.ToInt32(txtPoints.Text)
        For i = Points.Length - 1 To 0 Step -1
            If (Points(i) > val) Then
              lblGrade.Text = Grade(i).ToString()
              ' Kill the loop
              i = 0
            End If 
        Next
    End Sub

End Class

Open in new window

This code needs a lot more checks and there are better ways to check the range but you can get the basic idea.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The user enters a grade into your TextBox called "txtPoints".  That grade can be retrieved from the TextBox using:

    txtPoints.Text

This returns a STRING, though, which can't be compared to your Array of INTEGER called "Points".

Thus, to determine where on the scale the entered grade lies, we must first convert that String to an Integer.  The method proposed by brutaldev will throw an exception and cause your application to crash if the user enters something that cannot be converted to an Integer (like "epic fail!").

The preferred method is to use Integer.TryParse() like this:

    Dim pts As Integer
    If Integer.TryParse(txtPoints.Text, pts) Then

        ' ... if we get in here then "pts" now has a valid Integer in it ...

        ' Now use your arrays to figure out where on the scale that value is:
        Dim Points() As Integer = {450, 400, 350, 300, 0}
        Dim Grade() As String = {"A", "B", "C", "D", "F"}

        ' ... put some code in here ...

    Else
        MessageBox.Show("Invalid Points Entered")
    End If
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you required to use the "parallel arrays?...or can you use something else?
0
 
MkellinyAuthor Commented:
We are required to use the parallel arrays.
0
 
roshnipatelCommented:
I would recommend using the Dictionary Object...


  Private Sub Display_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        lblGrade.text = GetGrade(Integer.Parse(txtPoints.Text))
    End Sub

    Private Function GetGrade(ByVal points_value As Integer) As String
        'assign array values
        Dim Points As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
        Dim value As String = ""
        Dim blnSuccess = False

        Points.Add(450, "A")
        Points.Add(400, "B")
        Points.Add(350, "C")
        Points.Add(300, "D")
        Points.Add(0, "F")

        blnSuccess = Points.TryGetValue(points_value, value)    'returns a True or False if it was able to find, and set value = Grade

        
        If blnSuccess Then 'you can handle if it found it or not here

        Else

        End If
        GetGrade = value    'return value
    End Function

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
brutaldev basically did it already.  Personally, I would loop forwards instead of backwards.  Here it is with Integer.TryParse():
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim pts As Integer
        If Integer.TryParse(txtPoints.Text, pts) Then
            If pts >= 0 Then
                Dim Points() As Integer = {450, 400, 350, 300, 0}
                Dim Grade() As String = {"A", "B", "C", "D", "F"}
                For i As Integer = 0 To Points.Count - 1
                    If pts >= Points(i) Then
                        lblGrade.Text = Grade(i)
                        Exit For
                    End If
                Next
            Else
                MessageBox.Show("Cannot have a negative point value")
            End If
        Else
            MessageBox.Show("Invalid Points Entered")
        End If
    End Sub

Open in new window

0
 
MkellinyAuthor Commented:
Ok, I followed your directions and I am getting a no source code available error?  Nothing is running
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
?

Post your current code...
0
 
MkellinyAuthor Commented:
Nevermind I worked it out I had the Arrays in the wrong place They where under the btnExit sub procedure.
0
 
MkellinyAuthor Commented:
Thank you Idle_Mind and BrutalDev I appreciate your guys help, I just kinda wish that I didn't get the answer would have liked to work it out on my own with an example or something, but you two are EXTREMELY appreciated nonetheless! Thank you so much!
0
 
MkellinyAuthor Commented:
Great help !
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"I just kinda wish that I didn't get the answer would have liked to work it out on my own "

I agree and was working in that direction with my initial posts but brutaldev jumped the gun.  =)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now