JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Classes and Objects

We've already become familiar with the idea behind classes and objects, as discussed in Chapter 2. The idea is that classes are a type, and objects are examples or instances of that class. The relationship between classes and objects is much like the relationship between cookie cutters and cookies—you use the cookie cutter to create new cookies. Just think of the numeric data types like Integer, which is a type, and a specific integer variable, myInteger233, which is an instance of that type. In fact, the Integer type is a class in Visual Basic, and variables of that type are in fact objects.

It's easy to create classes and objects in Visual Basic. To create a class, you only need to use the Class statement, which, like other compound statements in Visual Basic, needs to end with End Class:

Public Class DataClass
End Class

This creates a new class named DataClass. You can create an object of this class, data, like this—note that you must use the New keyword to create a new instance of a class:

Dim data As New DataClass()

You also can do this like this:

Dim data As DataClass = New DataClass()

That's all there is to it, but of course, not much is happening here. It's when you start giving classes their own methods, fields, properties, and events that things become more useful.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor