Home -> Programming -> Objects -> Creating an Object

Creating an Object

Note! You should be familiar with variables, procedures and functions before you try using objects.

An object is made by creating a class. A class is a blueprint for an object. Since we can and often do use several copies of an object in the same program, we need a way to specify what the basic properties and methods of our object is. For example, there are very few houses today which have only one window. We can speak of a window as an abstract idea (an object) to let in light, and perhaps fresh air, into our homes. However, when we quit speaking about the concept and start installing and using (implementing) windows, we put several of them in our homes. at least one in every room. So each time we use the object, we create an instance of the object.

A class is created by selecting 'Add a Class' from the project menu. We are asked to name the file and should carefully choose a name which describes the generic object. Do not make the name too long since we will likely use it several times in our programs and have to retype it each time. If you fail to rename the class when it is first created, and the window containing it opens, it will look like this:

Public Class MyClass

End Class

This code is very simple, there is nothing there, but we have a class with the name MyClass. (This is a very poor name for a class since it says nothing about what the class is or what it can do for us! So be sure to choose a more descriptive name rather than accepting the default name!) Now we can proceed to fill in the guts of the object and make it into something real and useful.

An object is a code module with two parts. When we create a new object we need to define each part

Part One, A set of variables which lists and describes the object's properties. These should normally always be declared as private variables so that no other code has access to them. All access to these variables is done through the object's methods which is part 2.

Part Two, A set of methods which are the object's behaviours - the things the object can do. At a minimum this should include a Setter and a Getter for each variable declared. We add other methods according to what behaviors we need the object to have.

To see how this works I will create a simple object, its variables and methods. As an example let us look at a person object which only holds and returns a name.

Creating an object named Person

  1. Select Add a Class from the Project menu.
  2. Change the name of the object from MyClass to Person. The resulting code should look like this:
    Public Class Person

    End Class
  3. Inside the object declaration, declare the following variables: Note! variables should all be private!
    Public Class Person
         Private name as String


  4. End Class
  5. For each variable we declare, we create a Getter, used by the programmer to get data from the object, and a Setter, used by the programmer to put data into the object. These methods must be Public so they can be used by the programmer in other modules.

    Public Sub SetName (ByVal firstName as String, ByVal lastName as String)
         'Setter receives data from the parameters and puts it into Name
         'Note: Setters are always Subs
         name = firstName & " " & lastName    'Concatentate firstName, a space, lastName & assign result to the name property
    End Sub

    Public Function GetName
         'Send the data in Name back to the calling routine
         'Note: Getters are always Functions
         Return name
    End Function
That's it! In principle creating an object is simple and straight forward. Of course we can construct very complex objects and the coding would reflect that complexity, but the basic process of creating an object is simple.
  1. Create the class,
  2. Name the class,
  3. Declare the variables and
  4. Code the methods.
The simple Person object would look like this.
Public Class Person

     Private name as String

     Public Sub SetName (ByVal firstName as String, ByVal lastName as String)
          'Setter receives data from the parameters, could error check and puts it into name
          name = firstName & " " & lastName
     End Sub

     Public Function GetName
          'Send the data in n\ame back to the calling routine
          Return name
     End Function

End Class

Note! In actual use, Getters and Setters are not used because a simpler and better method has been developed. So this section is for explanation only and should not be used for coding. The better method is a Property Declaration and it is explained in the next section.