Structure...End Structure Statement

   
Structure...End Structure Statement

Syntax

   accessmodifier   Structure   StructureName   [Implements   interfacenames   ]    variable declarations    procedure declarations End Structure 
accessmodifier (optional; Keyword)

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

Implements interfacenames (optional)

Indicates that the structure implements the members of one or more interfaces

Description

Used to declare user -defined types. Structures are similar to classes, but they are value types rather than reference types.

Rules at a Glance

  • The members of a structure can be variables , properties, methods , or events. Note, however, that each member must be declared with an access modifier: Public (or Dim ), Private , or Friend .

  • You cannot assign a structure member an initial value at the same time as you declare it. As a result, the following Structure construct is illegal:

     Structure Point    Public x As Integer = 0       ' Illegal    Public y As Integer = 0       ' Illegal End Structure 
  • Structure members can be other structures or objects.

  • If a structure member is an array, it cannot be explicitly dimensioned.

  • Structures can be passed as arguments to functions or as the return type of a function.

  • Although structures are similar to classes, the following class features are not supported in structures:

    • Structures cannot explicitly inherit, nor can they be inherited.

    • All constructors for a structure must be parameterized.

    • Structures cannot define destructors.

    • Member declarations cannot include initializers, nor can they use the As New syntax or specify an initial array size .

Example

The simplest and most common use of structures is to encapsulate related variables. For instance, we might define a structure as follows :

 Structure strPerson     Public Name As String     Public Address As String     Public City As String     Public State As String     Public Zip As String     Public Age As Short End Structure 

To define a variable of type strPerson , we write (as usual):

 Dim APerson As strPerson 

To access a member of a structure, we use the dot syntax, as in:

 APerson.Name = "Beethoven" 

Programming Tips and Gotchas

  • Related items of information are often stored in multiple arrays (or in a multidimensional array). However, it is often preferable to store related data in a single array of structures.

  • The Structure statement is often used to define a data structure capable of retrieving, storing, and saving fixed-length records. However, this is complicated by the absence of support for explicitly declared fixed-length strings in VB.NET. One solution is to use the <vbFixedString( length )> attribute, where length is the fixed length of the string, when defining a member of type String. This instructs the VB.NET compiler to enforce a particular string length for the structure. For example:

     Structure Person    <vbFixedString(10)> Public FName As String    <vbFixedString(2)>  Public MName As String    <vbFixedString(10)> Public LName As String    Public Age As Short End Structure 

VB.NET/VB 6 Differences

  • The Structure...End Structure construct is new to VB.NET. It replaces the Type...End Type construct in VB 6.

  • VB 6 user-defined types are different than VB.NET structures. A VB 6 user-defined type is simply a composite data type that combines multiple data types; it allows the user-defined type to be treated as a contiguous, word- or double-word aligned block of memory. A VB.NET structure is in some sense a hybrid object that combines data types and methods; ordinarily, no assumptions should be made about its layout in memory.

  • In VB 6, the declaration of user-defined type members did not permit an access modifier. In VB.NET, it is required.

   


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