Appendix K
Classes and Structures
This appendix provides information about class and structure declarations.
The syntax for declaring a class is:
[attribute_list] [Partial] [accessibility] [Shadows] [inheritance] _
Class name [(Of type_list)]
[Inherits parent_class]
[Implements interface]
statements
End Class
The attribute_list can include any number of attribute specifiers separated by commas.
The accessibility clause can take one of the following values: Public, Protected, Friend, Protected Friend, and Private.
The Partial keyword indicates that this is only part of the class declaration and that the program may include other partial declarations for this class.
The Shadows keyword indicates that the class hides the definition of some other entity in the enclosing class’s base class.
The inheritance clause can take the value MustInherit or NotInheritable.
The type_list clause defines type parameters for a generic class. For information on generics, see Chapter 26, “Generics.”
The Inherits statement tells the class from which this class inherits. A class can include at most one Inherits statement and, if present, this must be the first non-comment statement after the Class statement.
The Implements statement specifies an interface that the class implements. A class can implement any number of interfaces. You can specify interfaces in separate Interface statements or in a single statement separated by commas.
The following example declares a simple Person class and an Employee class that inherits from it:
Public Class Person
End Class
Public Class Employee
Inherits Person
End Class
The syntax for writing a structure is as follows:
[attribute_list] [Partial] [accessibility] [Shadows] _
Structure name [(Of type_list)]
[Implements interface]
statements
End Structure
The structure’s attribute_list, Partial, accessibility, Shadows, type_list, and Implements statements are the same as those for classes. See the previous section for details.
The major differences between a structure and a class are:
A constructor is a special subroutine named New.
Class constructors can take any number of parameters. If you provide no constructors, Visual Basic allows a default empty constructor that takes no parameters. If you provide any constructor, Visual Basic does not provide a default empty constructor. If you want to allow the program to use an empty constructor in that case, you must either provide one or provide a constructor with all optional parameters.
Structure constructors are very similar to class constructors with two major exceptions. First, you cannot make a structure constructor that takes no parameters. Second, Visual Basic always provides a default empty constructor, even if you give the structure other constructors.
An event lets an object notify the application that something potentially interesting has occurred.
The syntax for declaring an event is:
[accessibility] [Shadows] Event event_name(parameters)
The accessibility clause can take one of the following values: Public, Protected, Friend, Protected Friend, or Private.
Use the Shadows keyword to indicate that the event shadows an item with the same name in the parent class. Any type of item can shadow any other type of item. For example, an event can shadow a subroutine, function, or variable. This would be rather bizarre and confusing, but it is possible.
The parameters clause specifies the parameters that you will pass when raising the event. An event handler catching the event will receive those parameters. Use ByRef parameters to allow the event handler to provide feedback to the code that raises the event.
The syntax for raising an event is as follows:
RaiseEvent event_name(arguments)
The arguments that you pass to the event handler must match the parameters declared in the Event statement.
The following code shows pieces of a SeatAssignment class that raises a NameChanged event when its Name property changes:
Public Class SeatAssignment
Public Event NameChanged()
...
Private m_Name As String
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
RaiseEvent NameChanged()
End Set
End Property
...
End Class