繁体   English   中英

(VISUAL BASIC 2015) 数组/多维数组

[英](VISUAL BASIC 2015) Arrays/Multidimensional Arrays

我基本上是一个初学者,所以我会尽可能简短。 如果我的某些语言被关闭,我深表歉意,因为某些概念甚至可能还没有出现在我的视线中。

我从 James Foxall 的介绍书“Teach Yourself Visual Basic in 24 Hours”和网络探索中了解到我对 VB 的了解。 我最近决定自己创建一个项目来练习数组。

我的项目很简单,由一个主窗体、2 个文本框(一个用于输入名字和姓氏)和一个用于将文本框的文本输入到多维数组中以供检索的按钮组成,其中一个消息框显示您的第一个和最后一个name 使用存储在数组中的信息。

下面是项目的代码示例,在这个配置中它运行良好。

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 的书中看到的内容和我在网络上看到并根据我的目的改编的代码片段之间创建了我的项目 - 或者至少尝试过。

我需要这方面的帮助,因为我被卡住了,我真的很想完成这项工作,盯着编码窗口并不会透露答案。 也不一定要寻找更多可能有效也可能无效的代码片段——这可能会破坏项目,以至于我不得不放弃它,因为我在这一点上缺乏理解来纠正甚至会更多先进的人一个简单的错误。

以下是该项目当前版本的片段。 注意:在底部,我注释掉了一些代码语句,以保留它们的位置。 如果这些代码中的一些看起来很愚蠢,我深表歉意,我实际上是在处理我从未使用过的东西,并且在我去的时候弄乱并让事情正常工作 - 或者没有让他们工作。 哈哈谢谢!

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

我建议放弃多维数组,而是使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM