Appendix K. Classes and Structures

This appendix provides information about class and structure declarations.

CLASSES

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 29, "Generics."

The Inherits statement tells which class this class inherits from. 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

STRUCTURES

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 differences between a structure and a class are:

  • Structures cannot use the MustInherit or NotInheritable keyword (because you cannot inherit from a structure).

  • Structures cannot use the Inherits clause.

  • Structures must contain at least one instance variable or event, which may be private. Strangely, a property procedure is not enough.

  • Structures are value types, whereas classes are reference types. See Chapter 26, "Classes and Structures," for information on the consequences of this difference.

CONSTRUCTORS

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.

Example program Constructors, which is available for download on the book's web site, defines a Person class that includes both empty and non-empty constructors, and demonstrates different ways of creating and initializing objects.

Structure constructors are very similar to class constructors with two major exceptions. First, you cannot make an empty structure constructor. Second, Visual Basic always provides a default empty constructor, even if you give the structure other constructors.

EVENTS

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(parameters)

The parameters that you pass to the event handler must match those 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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.47