0

The following is the code which I am using for creating a two dimensional array

Public Class frmGrades
Private Score As Dictionary(Of String, String) = New Dictionary(Of String, String)

Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
    Static intNumber As Integer
    ReDim Preserve Score(1, intNumber)
    Score(0, intNumber) = txtStudent.Text
    Score(1, intNumber) = txtGrade.Text
    hsbStudent.Maximum = intNumber
    hsbStudent.Value = intNumber
    intNumber = intNumber + 1
    txtGrade.Clear()
    txtStudent.Clear()
    txtStudent.Focus()

End Sub

Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
    txtStudent.Text = Score(0, hsbStudent.Value)
    txtGrade.Text = Score(1, hsbStudent.Value)

End Sub

Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
    txtStudent.Text = Score(0, hsbStudent.Minimum)
    txtGrade.Text = Score(1, hsbStudent.Minimum)

End Sub

Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
    txtStudent.Text = Score(0, hsbStudent.Maximum)
    txtGrade.Text = Score(1, hsbStudent.Maximum)
End Sub

**Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click

    If Score.ContainsKey(txtStudent.Text) Then
        Score.Item(txtStudent.Text) = txtGrade.Text
    Else
        Score.Add(txtStudent.Text, txtGrade.Text)
    End If
End Sub

End Class**

Now I would like to edit the grade text box which should also change the grade in the array. Any idea on how could this be done.

1 Answer 1

1

UPDATE:

OK, I see the code has a little more now and the problem is more clear as well as the need for a different approach. I rewrote your code. See if this works for you:

Public Class frmGrades

    Public Class StudentGrade
        Public Name As String
        Public Grade As String
    End Class

    Private Score As List(Of StudentGrade) = New List(Of StudentGrade)

    Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click

        AddStudent()

    End Sub

    Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
        Update(hsbStudent.Value - 1) 'lists are zero-based
    End Sub

    Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
        Update(0)
    End Sub

    Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
        Update(hsbStudent.Maximum - 1)
    End Sub
    Private Sub AddStudent()
        Dim nm As New StudentGrade
        nm.Name = txtStudent.Text
        nm.Grade = txtGrade.Text
        Score.Add(nm)

        hsbStudent.Minimum = 1
        hsbStudent.Maximum = Score.Count 
        hsbStudent.Value = Score.Count 

        txtGrade.Clear()
        txtStudent.Clear()
        txtStudent.Focus()
    End Sub
    Private Sub Update(i As Integer)
        txtStudent.Text = Score(i).Name
        txtGrade.Text = Score(i).Grade
    End Sub
    Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click

        Dim index As Integer = -1, cnt As Integer = 0

        For Each nm As StudentGrade In Score
            If nm.Name.ToLower = txtStudent.Text.ToLower Then
                index = cnt
                Exit For
            End If
            cnt += 1
        Next

        If index = -1 Then
            AddStudent()
        Else
            Score(index).Name = txtStudent.Text
            Score(index).Grade = txtGrade.Text
            hsbStudent.Value = index + 1
        End If

    End Sub

End Class

With this code you can expand the class StudentGrade to contain whatever you need to store. The list, contrary to dictionary, allow you to use index values as well.

Sign up to request clarification or add additional context in comments.

6 Comments

I forgot to mention, the edit is a new button itself . so do you think I can still use the dictionary there. The code I pasted above was to feed values in the array. Now I want to change any value and then click on the edit button to change those values in the array.
Certainly, if you put the dictionary reference in class level you can refer it from any of your buttons within the same class.
Ok, lets see - What exactly doesn't work? Do you get an error message? (please post). I'll edit my answer to make it more clear
Senthil, I saw in my original answer that I had left a "student" value instead of txtStudent.Text. Please replace if you're using the code as-is. I have updated the original example.
For redim statement it gives me an error "Redim requires an array." Also for the txtStudent.Text it say too many arguments
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.