VB Structures

Structures

Sometimes you may need to define your own data type in your VB code -- structures allow you to create composite values.

Primitive variables always hold one value related to one piece of information. For example:

Dim employeeID As Integer
Dim employeeFirstName As String
Dim employeeStartDate As Date


VB uses a structure to group related variables together to make a new data type. The VB keywords Structure and End Structure allow you to define a structure.

Structure EmployeeRecord
  Dim employeeID As Integer
  Dim employeeFirstName As String
  Dim employeeStartDate As Date
End Structure


By default a Structure is Public but can be made Private or Friend.

' Module-level declarations.
Private Structure Product
  Dim productID As Integer
  Dim productSKU As String
End Structure


Structures cannot be declared inside a procedure.

Structures are usually placed at the top of a file with other module-scope declarations or in a separate file.

Once you have created a structure, you can declare variables of that structure.

Dim permEmployee As EmployeeRecord
Dim officeEmployee(100) As EmployeeRecord


Each field of data in a structure is referred to as an element of the structure. To access elements use the dot notation similar to that used for objects as in Variable.Element

permEmployee.employeeID = 100 permEmployee.employeeFirstName = "John" permEmployee.StartDate = Date.Now


Arrays can be included as elements within a Structure. VB does not allow you to declare the number of elements in the array within the Structure declaration. Use the ReDim keyword inside a procedure to define the size of the array.

' Module-level declarations.
Private Structure Product
  Dim productID As Integer
  Dim productSKU As String
  Dim priceHistory( ) As Decimal
End Structure

Private warehouse As Product

Public Sub findCheapestPrice()
  ' Set the number of elements in the array.
  ReDim warehouse.priceHistory(10)
End Sub



Although it may lead to complicated coding, structures may be defined inside other structures. The dot notation in this case would apply left to right. Structures may not be defined within their own structures (no recursive structures).

Private Structure Product
  Dim productID As Integer
  Dim productSKU As String
  Dim priceHistory( ) As Decimal
End Structure

Structure warehouse
  Dim prodList() As Product
End Structure

Public Sub findCheapestPrice()
  Dim warehouseBC As warehouse
  ReDim warehouseBC.prodList(5)
  warehouseBC.prodList(0).productID = 10
End Sub


Structures are similar to classes -- they both have properties and procedures, and can implement interfaces, and they can have parameterized constructors. They differ in that structures are value types while classes are reference types. Structures use stack allocation; classes use heap allocation. Structures are not inheritable; classes are. A structure does not require a constructor; a class does.

Public Structure employee

  ' Public members, accessible from throughout declaration region.
  Public firstName As String
  Public middleName As String
  Public lastName As String

  ' Friend members, accessible from anywhere within the same assembly.
  Friend employeeNumber As Integer
  Friend workPhone As Long

  ' Private members, accessible only from within the structure itself.
  Private homePhone As Long
  Private level As Integer
  Private salary As Double
  Private bonus As Double

  ' Procedure member, which can access structure's private members.
  Friend Sub calculateBonus(ByVal rate As Single)
    bonus = salary * CDbl(rate)
  End Sub

  ' Property member to return employee's eligibility.

  Friend ReadOnly Property eligible() As Boolean
    Get
      Return level >= 25
    End Get
  End Property

  ' Event member, raised when business phone number has changed.
  Public Event changedWorkPhone(ByVal newPhone As Long)

End Structure


More information at http://msdn.microsoft.com/en-us/library/k69kzbs1%28v=vs.100%29.aspx

posted @ 2015-06-17 14:59  xymum  阅读(158)  评论(0编辑  收藏  举报