Export (0) Print
Expand All

Structures and Other Programming Elements

You can use structures in conjunction with arrays, objects, and procedures, as well as with each other. The interactions use the same syntax as these elements use individually.

Note   You cannot initialize any of the structure members in the structure declaration. You can assign values only to members of a variable that has been declared to be of a structure type.

Structures and Arrays

A structure can contain an array as one of its elements, as shown in this example:

Public Structure SystemInfo
   Public CPU As String
   Public Memory As Long
   Public DiskDrives() As String   ' Array of changeable size.
   Public PurchaseDate As Date
End Structure 

You access the values of an array within a structure the same way you access a property on an object, as shown in this example:

Dim MySystem As SystemInfo
ReDim MySystem.DiskDrives(3)
MySystem.DiskDrives(0) = "1.44 MB"

You can also declare an array of structures, as shown in this example:

Dim AllSystems(100) As SystemInfo

You follow the same rules to access the components of this data architecture, as shown in this example:

ReDim AllSystems(5).DiskDrives(3)
AllSystems(5).CPU = "386SX"
AllSystems(5).DiskDrives(2) = "100M SCSI"

Structures and Objects

A structure can contain an object as one of its elements, as shown in the following example:

Protected Structure UserInput
   Public UserName As String
   Public InputForm As System.Windows.Forms.Form
   Public UserFileNumber As Integer
End Structure 

You should use a specific object class in such a declaration, rather than Object.

Structures and Procedures

You can pass a structure as a procedure argument, as shown in this example:

Public CurrentCPUName As String = "700MHz Pentium compatible"
Public CurrentMemorySize As Long = 256
' ...
Public Sub FillSystem(ByRef SomeSystem As SystemInfo)
   SomeSystem.CPU = CurrentCPUName
   SomeSystem.Memory = CurrentMemorySize
   SomeSystem.PurchaseDate = Now
End Sub

The preceding example passes the structure by reference, which allows the procedure to modify its elements so that the changes take effect in the calling code. If you want to protect a structure against such modification, pass it by value.

You can also return a structure from a function procedure, as shown in this example:

Dim AllSystems(100) As SystemInfo
' ...
Function FindByDate(ByVal SearchDate As Date) As SystemInfo
   Dim I As Integer
   For I = 1 To 100
      If AllSystems(I).PurchaseDate = SearchDate Then Return AllSystems(I)
   Next I
   ' Process error: system with desired purchase date not found.
End Function

Structures Within Structures

Structures can contain other structures, as shown in the following example:

Public Structure DriveInfo
   Public Type As String
   Public Size As Long
End Structure 
' ...
Public Structure SystemInfo
   Public CPU As String
   Public Memory As Long
   Public DiskDrives() As DriveInfo   ' Array of changeable size.
   Public PurchaseDate As Date
End Structure 
' ...
Dim AllSystems(100) As SystemInfo
ReDim AllSystems(1).DiskDrives(3)
AllSystems(1).DiskDrives(0).Type = "Floppy"

You can also use this technique to encapsulate a structure defined in one module within a structure defined in a different module.

Structures can contain other structures to an arbitrary depth.

See Also

Structures: Your Own Data Types | Structure Variables | Structures and Classes | Function Procedures | Procedure Arguments | Structure Statement

Show:
© 2015 Microsoft