vb.net 二维数组 VB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21224292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:29:34  来源:igfitidea点击:

Two Dimensional Array VB

arraysvb.netmultidimensional-arraytextbox

提问by VinceCat

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.

好的,所以我在将元素添加到我的二维数组中时遇到了问题。我使用 3 个文本框来允许用户将项目输入到我的数组中。我的问题是我似乎无法让数组超过 (0,2)。我希望用户能够在每次单击添加按钮时添加一行输入。到目前为止,这是我的代码中的内容。任何人都可以帮忙吗?这不是我自己学习的课程。

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

回答by chris_techno25

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).

试试这个……你的计数变量必须放在 btnAdd_Click 子之外,否则它会始终重置为 0,因此,你不会超过 (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