Microsoft Office Tutorials and References
In Depth Information
Within a class, you’ll find a number of different things. You can define class-level variables,
which can be accessed from anywhere in the class. If a class-level variable is marked as Public ,
it becomes available to anyone using the class and is then known as a Property of the class.
In addition to public class-level variables, properties might also have code associated with
them. Each property where you want to use code is organized into two routines, which return
the value to the calling program or change the value in the class. The Get routine returns a
value to the caller, whereas the Let or the Set routine allows the caller to assign a value to
Remember that you can define a property with only a Get routine or a Let or Set routine. If
you use only a Get routine, the property becomes read-only and it’s value can’t be changed by
the program using the object. Likewise, if you include only a Let or a Set routine, the property
becomes write-only, and its value can’t be viewed by the calling program.
Along with class-level variables and property routines, a class can also contain a series of reg
ular subroutines and functions. If a function or a subroutine is marked as Public , it’s known
as a method . Methods can be invoked from code residing inside or outside of the class.
Events are subroutines that exist outside the code associated with the class that can be called
from statements within the class. Events provide a way for a class to interrupt the program
that created an instance of the object from the class, thereby allowing the program to perform
its own processing in response to a situation encountered by the object. Remember that the
code associated with an event actually resides outside the class. The only information stored
within the class is the event’s definition, including the parameters that will be passed to the
Note Classes containing events require the WithEvents keyword to be placed in the
object’s declaration. If the declaration doesn’t include this keyword, any events that occur
will be ignored by the external program.
Together these public properties, methods, and events of a class provide the interface to the
object. The interface isolates the code inside the class from the code that uses the objects cre
ated from this class. This isolation is highly desirable for several reasons. First, it allows you to
test the class independently of the rest of the application. Once you are satisfied that the class
is stable, you can treat it as a black box that simply works. Second, it makes it easier for mul
tiple people to work on a single application. One person can work on the class, while others
work on code that use the class. Third, it’s possible to modify the code inside the class without
necessarily impacting applications that use the class. This way you can implement new algo
rithms or add new features to the class without changing the programs that use the class.