Classes, Structures, and Modules

 <  Day Day Up  >  

The fundamental types are not always sufficient for a program's needs, so new types can be defined. The simplest type that can be defined is a module . Modules can contain members, but only one instance of the module ever exists in an application. Modules are equivalent to a class or structure with only shared members , and are ideal for purely procedural code.

 Module Test   Dim i, j As Integer   Function Add(ByVal x As Integer, ByVal y As Integer) As Integer     Return x + y   End Function   Sub Main()     i = 5     j = 10     Console.WriteLine(Add(i,j))   End Sub End Module 

Classes and structures , on the other hand, behave more like the fundamental types and may have multiple instances. Classes and structures are distinguished from each other primarily by the fact that they have different memory management behaviors: Classes are reference types, while structures are value types.

 Class C   Public i, j As Integer End Class Structure S   Public x, y As Integer End Structure Module Test   Sub Main()     Dim s1 As S  ` Declares an instance of structure S     Dim c1 As C  ` Declares a reference to class C     s1.x = 10    ` No need to create the structure because it is     s1.y = 20    ` a value type     c1 = New C() ` Because C is a class, it must be created before     c1.i = 10    ` it can be assigned to.     c1.j = 20   End Sub End Module 

Classes and structures can contain both instance and shared members. Each instance of a class or structure has its own copy of an instance member, while all classes and structures of a particular type share the same copy of a shared member. Because instance members are unique to each instance, they must be qualified with an instance of the type. Shared members are qualified with the type name .

 Class C   Public Shared x As Integer   Public y As Integer End Class Module Test   Sub Main()     Dim c1, c2 As C     c1 = New C()     c1.y = 10       ` Value is assigned to this instance only     C.x = 20        ` Value is assigned to all instances     c2 = New C()     Console.WriteLine(c2.y) ` Prints out 0     Console.WriteLine(C.x)  ` Prints out 20   End Sub End Module 

Accessibility

Members of user -defined types always have an accessibility level that determines what code can access the member. Public members are accessible by any code, while Private members are not accessible outside of the type that declares it. Friend members are accessible only within the containing assembly. There are two other accessibility levels, Protected and Protected Friend , which will be covered in the Inheritance section later in this chapter.

By default, all type members have Public access except for fields in classes and modules, which default to Private .

Constructors

Constructors are methods that are executed when an instance of a class or structure is created, allowing for user-defined initialization of the type. Constructors look exactly like regular methods , except that they have the special name New . For example:

 Class Customer   Dim Name As String   Dim Address As String   Dim PhoneNumber As String   Public Sub New(ByVal Name As String, ByVal Address As String)     MyBase.New()     Me.Name = Name     Me.Address = Address   End Sub End Class Module Test   Sub Main()     Dim Bob As Customer     Bob = New Customer("Bob", "134 Main Street")   End Sub End Module 

In this example, the Customer class defines a constructor that takes two parameters: a customer name and a customer address. It initializes the Name and Address fields with the values supplied. As shown in the example, arguments supplied to a New expression are passed to the constructor parameters. Like methods, constructors can be overloaded.

Every class or structure is required to have a constructor. If a class or structure doesn't define a constructor explicitly, the language assumes a default constructor, with no parameters, that does nothing. Each constructor must begin with a call to another constructor in the same class ( Me.New(...) ) or to another constructor in the base class ( MyBase-.New(...) ).

Constructors are primarily used to initialize instances of types, but they can also be used to initialize shared information in a type. A shared constructor is declared the same as a regular constructor except that it uses the Shared modifier, can have no parameters, and cannot be declared with an access level.

Nested Types

A type may be declared within another type and is called a nested type . For a nested type to be referred to from outside the type it is declared in, the type name must be qualified with the outer type's name.

 Class Customer   Class Address     ...   End Class   ... End Class Module Test   Sub Main()     Dim a As New Customer.Address()   End Sub End Class 

In this example, the address of a customer is stored in an Address type that is declared within the Customer type.

 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

Similar book on Amazon

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