2D Array Code

The following VB.NET project declares a 2D array and populates it when the form loads.

The first procedure scan the array row wise, then column wise.

The second procedure searches the array for a given surname (the search is case sensitive) and returns the whole row for that person if it is found.

Public Class Form1
    Dim stPeople(4, 8) As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        stPeople(0, 0) = "Barack"
        stPeople(1, 0) = "Obama"
        stPeople(2, 0) = "Male"
        stPeople(3, 0) = "American"
        stPeople(4, 0) = "President"

        stPeople(0, 1) = "Keira"
        stPeople(1, 1) = "Knightley"
        stPeople(2, 1) = "Famale"
        stPeople(3, 1) = "British"
        stPeople(4, 1) = "Actor"

        stPeople(0, 2) = "Audrey"
        stPeople(1, 2) = "Hepburn"
        stPeople(2, 2) = "Female"
        stPeople(3, 2) = "British"
        stPeople(4, 2) = "Actor"

        stPeople(0, 3) = "Albert"
        stPeople(1, 3) = "Einstein"
        stPeople(2, 3) = "Male"
        stPeople(3, 3) = "Swiss"
        stPeople(4, 3) = "Scientist"

        stPeople(0, 4) = "Usain"
        stPeople(1, 4) = "Bolt"
        stPeople(2, 4) = "Male"
        stPeople(3, 4) = "Jamaican"
        stPeople(4, 4) = "Athlete"

        stPeople(0, 5) = "Timothy"
        stPeople(1, 5) = "Peake"
        stPeople(2, 5) = "Male"
        stPeople(3, 5) = "British"
        stPeople(4, 5) = "Astronaut"

        stPeople(0, 6) = "Mahatma"
        stPeople(1, 6) = "Gandhi"
        stPeople(2, 6) = "Male"
        stPeople(3, 6) = "Indian"
        stPeople(4, 6) = "Politician"

        stPeople(0, 7) = "Brigitte"
        stPeople(1, 7) = "Bardot"
        stPeople(2, 7) = "Female"
        stPeople(3, 7) = "French"
        stPeople(4, 7) = "Actor"

        stPeople(0, 8) = "Vincent"
        stPeople(1, 8) = "Van Gogh"
        stPeople(2, 8) = "Male"
        stPeople(3, 8) = "Dutch"
        stPeople(4, 8) = "Artist"
    End Sub

    Private Sub TwoDArray_Click(sender As Object, e As EventArgs) Handles TwoDArray.Click

        Dim stOutput As String

        'scan the array row wise
        For y As Integer = 0 To 8
            For x As Integer = 0 To 4
                stOutput = stOutput & stPeople(x, y) & " "
            Next
            stOutput = stOutput & vbNewLine
        Next

        MsgBox(stOutput)

        stOutput = ""

        'scan the array column wise
        For x As Integer = 0 To 4
            For y As Integer = 0 To 8
                stOutput = stOutput & stPeople(x, y) & " "
            Next
            stOutput = stOutput & vbNewLine
        Next

        MsgBox(stOutput)

    End Sub

    Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click

        Dim iCount As Integer
        Dim stFind As String
        stFind = Me.txtFind.Text
        Dim bFound As Boolean

        For iCount = 0 To 8
            If stPeople(1, iCount) = stFind Then
                bFound = True
                Exit For
            End If
        Next

        If bFound = True Then
            MsgBox(stPeople(0, iCount) & " " _
                & stPeople(1, iCount) & " " _
                & stPeople(2, iCount) & " " _
                & stPeople(3, iCount) & " " _
                & stPeople(4, iCount))
        Else
            MsgBox(stFind & " not found")
        End If

    End Sub

End Class