Visual Basic Language Reference  

Class Statement

Declares the name of a class, as well as a definition of the variables, properties, events, and methods that comprise the class.

[ <attrlist> ] [ Public | Private | Protected | Friend | 
Protected Friend ] [ Shadows ] [ MustInherit | NotInheritable ] _
Class nameInherits classname ]
   Implements interfacenames ]
   [ statements ]
End Class

Parts

attrlist
Optional. List of attributes that apply to this class. Multiple attributes are separated by commas.
Public
Optional. Entities declared with the Public modifier have public access. There are no restrictions on the use of public entities.
Private
Optional. Entities declared with the Private modifier have private access. A private entity is accessible only within its declaration context, including any nested entities.
Protected
Optional. Entities declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.
Friend
Optional. Entities declared with the Friend modifier have friend access. An entity with friend access is accessible only within the program that contains the entity declaration. Classes that do not specify an access modifier are declared as Friend by default.
Protected Friend
Optional. Entities declared with the Protected Friend modifiers have the union of protected and friend accessibility.
Shadows
Optional. Indicates that this class shadows an identically named programming element in a base class. You can shadow any kind of declared element with any other kind. A shadowed element is unavailable in the derived class that shadows it
MustInherit
Optional. Indicates that non-shared members of the Class can only be accessed via derived classes. Instances of must-inherit classes cannot be created.
NotInheritable
Optional. Indicates that the Class is a class from which no further inheritance is allowed.
name
Required. Name of the Class; follows standard variable naming conventions.
Inherits
Optional. Indicates that this class inherits the members of another class. A class can inherit from only one other class.
classname
The name of base class that this class inherits.
Implements
Optional. Indicates that this class implements the members of an interface. If you use the Implements statement, it must immediately follow any Inherits statements after the Class statement, and you must implement every member defined by every interface you specify.
interfacenames
Required if the Implements statement is used. The names of the interfaces implemented by this class.
statements
Optional. Statements which comprise the variables, properties, events, methods and nested types of the class.
End Class
Terminates the Class block.

Each attribute in the attrlist part has the following syntax and parts:

attrname [({ attrargs | attrinit })]

attrlist Parts

attrname
Required. Name of the attribute. Must be a valid Visual Basic identifier.
attrargs
Optional. List of positional arguments for this attribute. Multiple arguments are separated by commas.
attrinit
Optional. List of field or property initializers for this attribute. Multiple initializers are separated by commas.

Remarks

Classes that do not specify an access modifier are declared as Friend by default. Within a Class block, members are declared as either Public, Private, Protected, Friend, or Protected Friend using the appropriate declaration statements. Anything declared as Private is visible only within the Class block. Anything declared as Public is visible within the Class block, as well as by code outside the Class block. Anything not explicitly declared is Public by default, except for fields and constants, which default to Private. Public variables, sometimes called Fields, serve as properties of the class, as do properties explicitly declared using Property declarations. Default properties and methods for the class are specified in their declarations using the Default keyword. See the individual declaration statement topics for information on how this keyword is used.

Binding of unqualified names in nested classes will search the members of the class itself, then the members of its containing class, and so on out to the outermost containing class. Private members of outer classes can be referenced but an error will be given for references to instance members of containing classes.

Nested classes cannot inherit from their containing class.

Example

This example uses the Class Statement to define a class, in which variables, properties, methods and events can be created.

Public Class ThisClass
  ' [variable, property, method and event declarations]
End Class

See Also

Inherits Statement | Implements Statement | Interface Statement | Property Statement | Object Lifetime: How Objects are Created and Destroyed | Understanding Classes