Class Statement

   
Class Statement

Syntax

 [   accessmodifier] [Shadows] [inheritability] Class Name     statements   End Class 
accessmodifier (optional; Keyword)

The possible values of accessmodifier are Public , Private , and Friend . For more information, see Section 4.7 in Chapter 4.

Shadows (optional; Keyword)

Indicates that the Name class shadows any element of this same name in a base class.

inheritability (optional; Keyword)

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.

Name (required; String literal)

The name of the class.

Description

Defines a class and delimits the statements that define that class' variables , properties, and methods . For a detailed discussion with examples, see Chapter 4.

Rules at a Glance

  • 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:

    Private variable or procedure declarations

    These items are accessible within the class, but do not have scope outside of the class.

    Public variable or procedure declarations

    Public variables are public properties of the class; Public procedures are public methods of the class.

    Property declarations

    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.

Programming Tips and Gotchas

  • 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.

VB.NET/VB 6 Differences

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.

See Also

Property Statement, Structure...End Structure Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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