Chapter 3. Class Anatomy

Encapsulation is the idea that data and the functions that manipulate that data are kept together. In Visual Basic .NET, encapsulation is achieved with the class, which can be thought of as the blueprint of an object. Classes contain member variables that are used to hold the state of an object—brightness, contrast, hue—and member functions (also known as methods) that describe the behavior or operations that can be performed on the object: turn on, turn off, or change the channel. They also are a source of events, which notify clients on the current affairs of the object.

Figure 3-1 illustrates the basic structure of a class, which contains four primary entities: member variables, methods (or member functions), properties, and events. As Example 3-1 shows, the class can exist as part of a namespace (or not).

Figure 3-1. Class overview
Example 3-1. Structure of a class block
Namespace [Namespace Name]
   
Public Class [Class Name]
   
    'Member variables
   
    'Methods
   
    'Properties
   
    'Events
    
End Class
   
End Namespace

Each class entity (with the exception of events) comes in two distinct flavors, instance or shared. Instance variables, methods, and properties operate on a specific instance of a class. When an object is created (instantiated), it receives its own copy of all the member data defined by the class. All method (and property) calls are made through that specific ...

Get Object-Oriented Programming with Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.