VinceCat
VinceCat

Reputation: 25

Two Dimensional Array VB

Ok so I am having problems adding elements into my 2d array. I am using 3 textboxes to allow a user to input items into my array. My problem is I cant seem to get the array to go past (0,2). I want the user to be able to add a row of inputs with each click of the add button. This is so far what I have in my code. Can anyone help? This is not for a class I am learning on my own.

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

Private strExams(49, 2) As String

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim strStudent As String = txtStudent.Text
    Dim strTest As String = txtTest.Text
    Dim strScore As String = txtScore.Text
    Dim count As Integer = 0

    If count <= 49 Then
        strExams(count, 0) = strStudent
        strExams(count, 1) = strTest
        strExams(count, 2) = strScore
        count += 1
    End If

    txtStudent.Text = String.Empty
    txtTest.Text = String.Empty
    txtScore.Text = String.Empty

    txtStudent.Focus()

End Sub

Upvotes: 0

Views: 7065

Answers (1)

chris_techno25
chris_techno25

Reputation: 2477

Try this... Your count variable must be placed outside the btnAdd_Click sub or it will always reset back to 0, thus, you won't get past (0,2).

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

Private strExams(49, 2) As String
Dim count As Integer = 0

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

Dim strStudent As String = txtStudent.Text
Dim strTest As String = txtTest.Text
Dim strScore As String = txtScore.Text

If count <= 49 Then
    strExams(count, 0) = strStudent
    strExams(count, 1) = strTest
    strExams(count, 2) = strScore
    count += 1
End If

txtStudent.Text = String.Empty
txtTest.Text = String.Empty
txtScore.Text = String.Empty

txtStudent.Focus()

End Sub

Upvotes: 1

Related Questions