【问题标题】:(VISUAL BASIC 2015) Arrays/Multidimensional Arrays(VISUAL BASIC 2015) 数组/多维数组
【发布时间】: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


    【解决方案1】:

    我建议放弃多维数组而使用List(Of T)。此外,我会创建一个类来保存人们的姓名,这样就可以扩展它而无需修改太多代码。

    看看这个例子:

    Public Class MainForm
        Private ReadOnly _people As New List(Of Person)()
    
        Private Sub btnInputName_Click(sender As Object, e As EventArgs) Handles btnInputName.Click
            If txtInputFirstName.Text.Length = 0 OrElse txtInputLastName.Text.Length = 0 Then
                MessageBox.Show("You must enter a first and last name please.")
                Exit Sub
            End If
    
            _people.Add(New Person() With {
                .FirstName = txtInputFirstName.Text,
                .LastName = txtInputLastName.Text
            })
    
            MessageBox.Show("Your first name is: " & _people.Last().FirstName & Environment.NewLine & "Your last name is: " & _people.Last().LastName & Environment.NewLine & "Thanks!")
        End Sub
    
        Private Sub RetrieveName_Click(sender As Object, e As EventArgs) Handles RetrieveName.Click
            For Each person In _people
                MessageBox.Show("Your first name is: " & person.FirstName & Environment.NewLine & "Your last name is: " & person.LastName)
            Next
        End Sub
    End Class
    
    Public Class Person
        Public Property FirstName As String
        Public Property LastName As String
    End Class
    

    【讨论】:

    • 非常感谢您的帮助。它向我介绍了新概念,这些新概念本身需要我进一步研究(我是初学者)。我可以遵循我所看到的大部分内容,我必须进行更多学习才能真正理解对我来说新的概念尽管。但这就是我们学习的方式不是吗?
    • @jonathan - 我强烈建议查看强类型对象(又名类),因为它们是面向对象编程的基础,这是 VB.NET 主要遵循的范例。此外,List(Of T) 非常有用,因为它们是您不需要提前知道长度(即集合中有多少项目)的集合,您只需根据需要添加/删除项目。如果您在“VB.NET 类”和“VB.NET List(Of T)”上使用搜索引擎,然后转到相关的 MSDN 文档,他们会更详细地查看它们。
    猜你喜欢
    • 2013-03-02
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多