VB Structures
2015-06-17 14:59
351 查看
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
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
相关文章推荐
- VB|xp风格:终于解决了“图片优化软件”在部分xp系统上无法启动的问题。
- VB: 再次使用的体会
- Vb.net/VB 声明API功能父窗口功能
- 【VB】删除MSHFlexGrid控件中一行(机房问题)
- 【VB】MDI子窗体显示(机房问题)
- 【VB】打开原机房(机房问题)
- 【VB】学生信息管理系统优化总结
- 【VB】学生信息管理系统问题总结
- [OpenGL ES 06]使用VBO:顶点缓存
- vba编程不知道该使用什么对象属性方法实现操作的时候用宏录制来查阅代码
- VB键盘鼠标无动作调用程序的尝试
- VB实现禁用任务管理器的方法
- VB简单实现防止文件被改写
- 用VB.NET实现AUTOCAD中鼠标选中后显示事件
- VB中 MSComm控件使用详解
- vb 函数中,如何使用双引号
- VBA类模块--添加类的函数(2)
- 《学习opencv》笔记——矩阵和图像操作——cvSum,cvSVD,cvSVBkSb,cvTrace,cvTranspose,cvXor,cvXorS and...
- VB外接API函数
- VB.Net中用户控件的使用