Class Statement |
[ accessmodifier] [Shadows] [inheritability] Class Name statements End Class
The possible values of accessmodifier are Public , Private , and Friend . For more information, see Section 4.7 in Chapter 4.
Indicates that the Name class shadows any element of this same name in a base class.
One of the keywords, MustInherit or NotInheritable , must be used. MustInherit specifies that objects of this class cannot be created, but that objects of derived classes can be created. NotInheritable specifies that this class cannot be used as a base class.
The name of the class.
Defines a class and delimits the statements that define that class' variables , properties, and methods . For a detailed discussion with examples, see Chapter 4.
If the Inherits or Implements statements appear in a class module, they must appear before any other statements in the module. Moreover, the Inherits keyword must appear before the Implements keyword.
Name follows standard Visual Basic variable-naming conventions.
Within a class code block, members are declared as Public , Private , Protected , Friend , or Protected Friend . The Dim keyword is equivalent to Private when used in class modules (but it is equivalent to Public in structures). Property declarations are automatically Public .
The Class...End Class construct can include the following elements:
These items are accessible within the class, but do not have scope outside of the class.
Public variables are public properties of the class; Public procedures are public methods of the class.
These are the public properties of the class. Default properties can be declared by using the Default keyword.
To define a custom constructor within a class module, define a subroutine called New . Note that the New subroutine (like any other procedure) can be overloaded.
To define a destructor within a class module, define a function called Destruct . Destructors cannot be overloaded.
To create an object of a class, use syntax such as:
Dim oObj As CClass oObj = New CClass(arguments_for_constructor) or: Dim oObj = New CClass(arguments_for_constructor) or: Dim oObj As CClass = New CClass( arguments_for_constructor )
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.
A property defined as a simple public variable cannot be designated the class' default member.
According to accepted object-oriented programming practices, public properties should be defined using the Property statement, since this allows the value of a property to be modified in a controlled and predictable way. It allows you to validate data and allows your program to know when a property value is being changed. Because this is not possible using simple public variables, defining a public variable that is accessible outside of the class is considered poor programming practice.
The Me or MyClass keywords can be used within the Class...End Class construct to reference the class.
The Class...End Class construct is new to VB.NET. In VB 6, each class was defined in its own class module, which corresponded to a separate CLS file.
Property Statement, Structure...End Structure Statement