Class...End Class Statement


Class...End Class Statement

Syntax

     [accessModifier] [Shadows] [MustInherit | NotInheritable]_           Class name [(Of typeParamName)]        [Inherits baseClass]        [Implements interfaceName[, interfaceName...]]        [statements]     End Class 


accessModifier (optional)

Specifies the scope and accessibility of the class. One of the following access levels:

Access level

Description

Public

The class is publicly accessible anywhere, both inside and outside of the project.

Private

The class is accessible within the type in which it is defined.

Protected

The class is accessible only to the type in which it is defined and to derived instances of that type.

Friend

The class is accessible only within the project that contains the structure definition.

Protected Friend

Combines the access features of Protected and Friend.


If omitted, the Friend access level is used.


Shadows (optional)

Indicates that the class shadows an identically named element in a base class.


MustInherit (optional)

This class can only be used as a base class for another derived class definition. Objects of this base class cannot be created.


NotInheritable (optional)

This class can only be used to create objects. It cannot be used as a base class for another derived class.


name (required)

The name of the class.


typeParamName (optional; any)

Adds type parameter placeholders that will later enforce strong typing when the class is used. The Of clause implements generics, which are fully described in Chapter 10. If generics will not be used, this clause can be excluded.


baseClass (optional)

Indicates that the class inherits from another class.


interfaceName (optional)

Indicates that the class implements the members of one or more interfaces.


statements (required)

Code that defines the members of the class.

Description

The Class...End Class statement defines a class and its members. Class members include fields, methods, properties, events, and other types.

Usage at a Glance

  • If the Inherits or Implements statements appear in a class module, they must appear before any other statements in the module. If both are used, the Inherits keyword must appear before the Implements keyword.

  • Class members are declared as Public, Private, Protected, Friend, or Protected Friend. The Dim keyword is equivalent to Private when used for members.

  • To add a custom constructor within a class module, define a subroutine called New. The New subroutine (like any other method) can be overloaded.

  • To add a destructor within a class module, override the Finalize method from the base System.Object class, from which all classes inherit.

         Protected Overrides Sub Finalize(  )        ' ----- Add other code here, and also call...        MyBase.Finalize(  )     End Sub 

    Destructors cannot be overloaded.

  • The Shadows keyword has the following meaning: if this class is derived from a base class and if name is used in the base class as the name of any element type (property, method, constant, enum, etc.), then any use of name in classes derived from the class name refers to the name class rather than the name element in the base class. For more on shadowing, see Chapter 4.

  • For more information on classes and object-oriented programming practices, see Chapter 3.

  • One class property can be assigned as the default property using the Default keyword with its definition.

  • The Me or MyClass keywords can be used within the Class...End Class construct to reference the class.

Version Differences

  • The Class...End Class construct is new to VB under .NET. In VB 6, each class was defined in its own class source code file. The syntax and functionality differences between them are significant.

  • Visual Basic 2005 adds support for generics to classes, as discussed in Chapter 10.

See Also

Property Statement, Structure...End Structure Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net