【发布时间】:2021-10-03 13:32:09
【问题描述】:
我基本上是一个初学者,所以我会尽可能简短。如果我的某些语言有问题,我深表歉意,因为某些概念甚至可能还没有出现在我的雷达上。
我从 James Foxall 的介绍性书籍“24 小时内自学 Visual Basic”和网络探索中了解到了我对 VB 的了解。我最近决定自己创建一个项目来练习数组。
我的项目很简单,由一个主窗体、2 个文本框(一个用于输入名字和姓氏)和一个按钮用于将文本框的文本输入到多维数组中以进行检索,消息框从中显示您的内容名字和姓氏使用存储在数组中的信息。
下面是项目的代码示例,在这个配置下运行良好。
Public Class MainForm
Dim strNameArray(10, 10) As String
Dim blnFullName As Boolean = False
Private Sub btnInputName_Click(sender As Object, e As EventArgs) Handles btnInputName.Click
If txtInputFirstName.Text.Length > 0 And txtInputLastName.Text.Length > 0 Then
blnFullName = True
Else
MessageBox.Show("You must enter a first and last name please.")
Exit Sub
End If
If blnFullName = True Then
strNameArray(0, 0) = txtInputFirstName.Text
strNameArray(0, 1) = txtInputLastName.Text
MessageBox.Show("Your name has been succesfully entered, thanks!")
txtInputFirstName.Clear()
txtInputLastName.Clear()
Else
MessageBox.Show("You must enter a first and last name please.")
Exit Sub
End If
MessageBox.Show("Your first name is: " & strNameArray(0, 0) & vbCrLf & "Your last name is: " & strNameArray(0, 1) & vbCrLf & "Thanks!")
End Sub
Private Sub btnRetrieveName_Click(sender As Object, e As EventArgs)
MessageBox.Show("Your first name is: " & strNameArray(0, 0) & vbCrLf & "Your last name is: " & strNameArray(0, 1) & vbCrLf & "Thanks!")
End Sub
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
结束类
但是,如上面的代码所示,信息是手动存储在数组中的,我真的想要更自动化的东西:我希望程序足够健壮,可以在每次输入时添加一个新的数组槽单击按钮,将输入的名称存储到新创建的数组槽中,然后按照输入的顺序读回输入的名称。这将允许用户输入多个名称(一个接一个)并将它们提交到数组,然后按另一个按钮来检索它们并通过消息框读回它们。
我基于这个目标创建了项目的一个版本,但是创建新数组槽的功能(我成功地做到了),特别是按顺序读回它们的功能(我没有成功)仍然躲着我。这对我来说是有问题的,因为这个功能超出了 Foxall 的书的范围,它提供了一个更基本的数组介绍。我创建了我的项目,混合了我在 Foxall 书中看到的内容,以及我在网络上看到并适应我的目的的一段代码——或者至少尝试过。
我需要帮助,因为我被困住了,我真的很想完成这项工作并且盯着编码窗口不会揭示答案。也不一定要四处寻找更多可能会或可能不会起作用的代码 sn-ps——这可能会破坏项目甚至到我不得不放弃它的地步,因为我在这一点上缺乏理解来纠正甚至会是对于更高级的人来说,这是一个简单的错误。
下面是项目当前版本的 sn-p。注意:在底部我有一些代码语句被注释掉,以保持它们的位置。抱歉,如果这些代码中的某些代码看起来很愚蠢,我实际上是在使用我从未使用过的东西,并且正在乱搞并让事情在我去的时候工作 - 或者没有让他们工作。大声笑,谢谢!
Private Sub btnInputName_Click(sender As Object, e As EventArgs) Handles btnInputName.Click
If txtInputFirstName.Text.Length > 0 And txtInputLastName.Text.Length > 0 Then
FullNameBool = True
Else
MessageBox.Show("You must enter a name please.")
Exit Sub
End If
If FullNameBool = True Then
strFirstNameArray(UBound(strFirstNameArray)) = txtInputFirstName.Text
strLastNameArray(UBound(strLastNameArray)) = txtInputLastName.Text
ReDim Preserve strFirstNameArray(UBound(strFirstNameArray) + 1)
ReDim Preserve strFirstNameArray(UBound(strFirstNameArray) + 1)
MessageBox.Show("Your name has been successfully entered, thanks!")
txtInputFirstName.Clear()
txtInputLastName.Clear()
Else
MessageBox.Show("You must enter a first and last name please.")
Exit Sub
End If
End Sub
Private Sub RetrieveName_Click(sender As Object, e As EventArgs) Handles RetrieveName.Click
For lngPositionFirstName = LBound(strFirstNameArray) To UBound(strFirstNameArray)
MessageBox.Show("Your first name is: " & strFirstNameArray(lngPositionFirstName))
Next lngPositionFirstName
For lngPositionLastName = LBound(strLastNameArray) To UBound(strLastNameArray)
MessageBox.Show(" Your last name is: " & strLastNameArray(lngPositionLastName))
Next lngPositionLastName
' For lngPositionLastName = LBound(strLastNameArray) To UBound(strLastNameArray)
' MessageBox.Show("Your first name is: " & strFirstNameArray(lngPositionFirstName) & vbCrLf & " Your last name is: " & strLastNameArray(lngPositionLastName))
End Sub
【问题讨论】:
标签: arrays vb.net multidimensional-array