18.1. Defining Classes


To define a new type or class you first declare it, and then define its methods and fields. You declare a class using the Class keyword. The complete syntax is:

 [attributes] [access-modifiers] Class identifier [inherits classname]    {class-body} End Class 

Attributes are used to provide special metadata about a class (that is, information about the structure or use of the class). You will not need attributes for routine Visual Basic 2005 programming.

Access modifiers are discussed later in this chapter. (Typically, your classes will use the keyword Public as an access modifier.)

The identifier is the name of the class that you provide. The optional Inherits statement implements specialization, and is explained below. The class body is defined until the End Class statement.

 Public Class Dog    Dim age as Integer        'the dog's age    Dim weight as Integer     'the dog's weight    Public Sub Bark(  )      '....    End Sub End Class 

All the things a dog can do (as far as your program is concerned) are described within the class definition of Dog, as are all the Dog's attributes (age and weight).



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

Similar book on Amazon

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