Procedures in Visual Basic

Open the program to show a cat or dog.

Download and save the picture of the bird or find one of your own.

Add BtnBird and PicBird with the picture of the bird.

The code for the new pets program is shown below:

'Programmer: Janet Joy
'Show picture of dog, bird or cat
Public Class Form1
    Private Sub BtnDog_Click(sender As Object, e As EventArgs) Handles BtnDog.Click
        hidepets() 'call the sub that hides all of the pets
        Me.PicDog.Visible = True
    End Sub

    Private Sub BtnCat_Click(sender As Object, e As EventArgs) Handles BtnCat.Click
        hidepets() 'call the sub that hides all of the pets
        Me.PicCat.Visible = True
    End Sub

    Private Sub BtnBird_Click(sender As Object, e As EventArgs) Handles BtnBird.Click
        hidepets() 'call the sub that hides all of the pets
        Me.PicBird.Visible = True
    End Sub

    Private Sub hidepets()
        'Hide all of the animals
        Me.PicCat.Visible = False
        Me.PicDog.Visible = False
        Me.PicBird.Visible = False
    End Sub
End Class

The code could also be written as shown below:

'Programmer: Janet Joy
'Show picture of dog or cat
Public Class Form1
    Private Sub BtnDog_Click(sender As Object, e As EventArgs) Handles BtnDog.Click
        hidepets() 'call the sub that hides all of the pets
        Me.PicDog.Visible = True
    End Sub

    Private Sub BtnCat_Click(sender As Object, e As EventArgs) Handles BtnCat.Click
        hidepets() 'call the sub that hides all of the pets
        Me.PicCat.Visible = True
    End Sub

    Private Sub hidepets()
        'Hide all of the animals
        Me.PicCat.Visible = False
        Me.PicDog.Visible = False
    End Sub
End Class

Compare these two programms. Now thing about what would happen if we added a couple more pets. The second solution will be much easier to add more animals to.

NEXT: Area