STRUCTURES

Structures are very similar to classes. The syntax for declaring a structure is as follows:

[attribute_list] [Partial] [accessibility] [Shadows] _
Structure name[(Of type_list)]
    [Implements interface]
    statements
End Structure

The only thing that all structure declarations must include is the Structure clause (including the structure’s name) and the End Structure statement. The rest is optional.

Unlike a class, however, a structure cannot be empty. It must contain at least one variable or event declaration. The following code defines a valid structure. Its only member is a Private variable, so this structure wouldn’t be of much use, but it is valid.

Structure EmptyStructure
    Private Num As Integer
End Structure

The structure’s attribute_list and accessibility clauses, Shadows and Partial keywords, and the Implements statement are the same as those for classes. See the earlier sections discussing these keywords for details.

There are two main differences between a structure and a class: Structures cannot inherit, and structures are value types rather than reference types.

Structures Cannot Inherit

Unlike a class, a structure cannot inherit so it cannot use the MustInherit, NotInheritable, or Inherits keywords; however, like a class, a structure can implement any number of interfaces. You can use interface inheritance to define inheritance-mimicking hierarchies of structures, and you can simulate multiple inheritance by making a structure implement multiple interfaces.

Get Visual Basic 2012 Programmer's Reference now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.