JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Structures and Modules

Classes are so popular in OOP that other programming constructs-in particular, structures and modules-are now based on them. Structures were originally a halfway solution between variables and true objects allowing you to create your own data types, much like adding fields to a class. As with classes, however, structures can now support methods. Modules in Visual Basic are designed primarily to hold code, but now they can also support members, just like classes.

Although structures support many of the same features as classes, including the ability to support fields, methods, events, and properties, it's important to realize that the following features of classes are not supported by structures:

  • Structures cannot explicitly inherit from any other type.

  • Structures cannot inherit from other structures.

  • You cannot define a nonshared constructor that doesn't take any arguments for a structure. You can, however, define a nonshared constructor that does take arguments. The reason for this is that every structure has a built-in public constructor without arguments that initializes all the structure's data members to their default values. That means that Dim employee As EmployeeStruct is the same as Dim employee As EmployeeStruct = New EmployeeStruct().

  • You cannot override the Finalize method in a structure.

  • The declarations of data members in a structure cannot include initializers, the New keyword, or set initial sizes for arrays.

  • If you declare them with Dim, the default access of data members in structures is public, not private as in classes and modules.

  • Structure members cannot be declared as Protected.

  • Structures are value types rather than reference types. This means, for example, that assigning a structure instance to another structure, or passing a structure instance to a ByVal argument causes the entire structure to be copied.

Tip 

You have to perform equality testing with structures testing member-by-member for equality.

In addition, modules are a reference type similar to classes, but with some important distinctions:

  • The members of a module are implicitly shared.

  • Modules can never be instantiated.

  • Modules do not support inheritance.

  • Modules cannot implement interfaces.

  • A module can be declared only inside a namespace.

  • Modules cannot be nested in other types.

  • You can have multiple modules in a project, but note that members with the same name in two or more modules must be qualified with the name of their module when used outside that module.

And now it's time to start digging into the specific details in the Immediate Solutions section of this chapter.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor