56. Classes

In this tutorial, we cover how to create a class, how to add members to a class and how to create an instance of a class. This is one of the core concepts of the language itself and it is vital that you understand it. A class is like a blank canvas or a template for the object that you will create. An object is an instance of a class, and so all objects are unique but similar. This concept may be difficult to grasp at first but should be easy enough if you stick at it.

Class Code

Public Class person

    Public firstname As String
    Public lastname As String
    Public eyecolour As String
    Public haircolour As String


    Public Sub setvalues(ByVal first As String, ByVal last As String, ByVal eye As String, ByVal hair As String)

        firstname = first
        lastname = last
        eyecolour = eye
        haircolour = hair

    End Sub

End Class

Code

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim bob As New person

        bob.firstname = "bob"
        bob.lastname = "smith"
        bob.eyecolour = "brown"
        bob.haircolour = "black"

        bob.setvalues("bob", "smith", "brown", "black")

        MessageBox.Show("Bobs hair colour is " & bob.haircolour)

    End Sub

End Class