' SmallBusiness 类模块中声明部分的代码。
Public Name As String
Public Product As New Product
Public Employees As New Collection
Public Customers As New Collection
第一次引用 Product 属性时,将创建对象,因为已经把它声明为 As New。例如,可以用下面的代码创建并设置 SmallBusiness 对象的 Product 对象的名称和价格。
' 一个标准模块的代码。
Public sbMain As New SmallBusiness
Sub Main
sbMain.Name = "Velociraptor Enterprises, Inc."
' 在代码中首次使用 Product 变量时,创建 Product 对象。
sbMain.Product.Name = "Inflatable Velociraptor"
sbMain.Product.Price = 1.98
.
. ' 初始化并显示主窗体的代码。
.
End Sub
Public Function NewEmployee(Name, Salary, HireDate, _
ID) As Employee
Dim empNew As New Employee
empNew.Name = Name '对象的隐含创建。
empNew.Salary = Salary
empNew.HireDate = HireDate
'添加到集合中,并使用 ID 作为一个键。
sbMain.Employees.Add empNew, CStr(ID)
'返回对新 Employee 的引用。
Set NewEmployee = empNew
End Function