(VISUAL BASIC 2015) Arrays/Multidimensional Arrays

am a total beginner basically, so I'll be as brief as possible. Apologies if some of my language is off, as certain concepts may not even be on my radar yet.

I learned what I know of VB from a combo of James Foxall's intro book "Teach Yourself Visual Basic in 24 Hours," and exploration on the web. I decided to create a project recently of my own to practice arrays.

My project is simple, consisting of one main form, 2 textboxes (one to input a first and last name), and one button to input the textboxes' texts into a multidimensional array for retrieval, from which a messagebox shows what your first and last name are using the info stored in the array.

Below is the code sample of the project, and in this configuration it works well.

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

End Class

However, as is visible in the code above, the info is being stored in the array manually, and I really want something that's more automated: I want the program to be robust enough to add a new array slot each time the input button is clicked, and store the inputted names into the newly created array slots, and then read back the name/names inputted in the order they were inputted. This would allow a user to input several names (one after another) and commit them to the array and then press another button to retrieve them and read them back via messagebox.

I created a version of the project with this goal in mind, but the functionality of creating new array slots (which I was successful at doing), and particularly reading them back in order (which I have not been successful at) still eludes me. This is problematic for me because this functionality is beyond the scope of Foxall's book, which presents a more basic intro to arrays. I created my project between a mixing of what i saw in Foxall's book, and a snippet of code that i saw and adapted for my purposes on the web-- or at least tried.

I need help on this because I'm stuck and I really want to make this work and staring at the coding window isn't going to reveal the answer. Nor is necessarily poking around for more code snippets that may or may not work-- that might break the project even more to the point where I'd have to scrap it because I lack the understanding at this point to correct even would be for more advanced people a simple mistake.

Below is a snippet of the current version of the project. Note: towards the bottom I have a few code statements commented out, to hold their place. Apologies if some of this code looks stupid, I was literally working with stuff I'd never used and was messing around and getting things to work as I went-- or not getting them to work. Lol, Thanks!

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

1 answer

  • answered 2021-07-27 20:31 David

    I would suggest abandoning a multidimensional array and instead use a List(Of T). Furthermore I would create a class to hold the people's names, that away it can be expanded without having to modify much code.

    Take a look at this example:

    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
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum