VBA Brush Up 08:Custom Collections and Class

Technorati 标签: , , ,

来自:Julitta Korol,“Access.2003.Programming.by.Example.with.VBA.XML.and.ASP”,by Wordware Publishing, Inc. 2005, p102-p141

(1)Event — An action recognized by an object, such as a mouse click or a keypress, for which you can define a response. Events can be caused by a user action or a VBA statement, or can be triggered by the system.

(2)Class Module — A module that contains the definition of a class, including its property and method definitions.

(3)Form Module — A module that contains the VBA code for all event procedures triggered by events occurring in a user form or its controls. A form module is a type of class module.

(4)Module — A structure containing subroutine and function procedures that are available to other VBA procedures and are not related to any object in particular.

(5)在立即窗口试验集合类的方法:

  1. set myTestCollection = New Collection 
  2. myTestCollection.Add "first member"
  3. For Each m in myTestCollection : myTestCollection.Remove 1 : Next

(6)

  1. Dim collection_name As New Collection
  2. object.Add item, key, before, after

The objects with which you populate your collection do not have to be of the same data type. You are only required to specify object and item. Object is the collection name. This is the same name that was used in the declaration of the Collection object. Item is the object you want to add to the collection.

items in a collection are automatically assigned numbers starting with 1. However, they can also be assigned a unique key value. Instead of accessing a specific item with an index (1, 2, 3, and so on) at the time an object is added to a collection, you can assign a key for that object.

If you want to specify the position of the object in the collection, you should use either the before or after argument (do not use both). The before argument is the object before which the new object is added. The after argument is the object after which the new
object is added.

(7)

  1. Sub NewEmployees()
  2.     ' declare the employees collection 
  3.     Dim colEmployees As New Collection 
  4.     ' declare a variable to hold each element of a collection 
  5.     Dim emp As Variant
  6.     ' Add 3 new employees to the collection 
  7.     With colEmployees 
  8.         .Add Item:="John Collins", Key:="128634456" 
  9.         .Add Item:="Mary Poppins", Key:="223998765" 
  10.         .Add Item:="Karen Loza", Key:="120228876", Before:=2 
  11.     End With 
  12.     ' list the members of the collection 
  13.     For Each emp In colEmployees 
  14.         Debug.Print emp 
  15.     Next
  16.     ' remove the third element from the collection 
  17.     colEmployees.Remove (3) 
  18.     MsgBox colEmployees.Count & " employees remain."
  19. End Sub

Note that the control variable used in the For Each…Next loop must be Variant or Object.

(8)Collections are reindexed automatically when an item is removed. Therefore, to remove all items from a custom collection you can use 1 for the Index argument, as in the following example:

  1. Do While myCollection.Count >0 
  2.     myCollection.Remove Index:=1 
  3. Loop

(9)Every time you create a new class module, give it a meaningful name. As a rule, the  object class name is prefaced with an uppercase “C.”

(10)Class variables are called data members and are declared with the Private statement. It’s traditional to preface the class variable names with m_ to indicate that they are data members of a class.

(11)To enable other parts of your VBA application to set or retrieve the employee data, you must add special property procedures to the CEmployee class module.

  1. Property Get LastName() As String
  2.     LastName = m_LastName
  3. End Property

Similar to function procedures, the Property Get procedures contain an assignment statement. As you recall, in order to return a value from a function procedure, you must assign it to the function’s name.

(12)Immediate Exit from Property Procedures: Exit Property

(13)

  1. Property Let LastName(L As String
  2.     m_LastName = L 
  3. End Property

The Property Let procedures require at least one parameter that specifies the value you want to assign to the property. This parameter can be passed by value (see the ByVal keyword in Property Let Salary above) or by reference (ByRef is the default). The data type of the parameter passed to the Property Let procedure must have exactly the same data type as the value returned from the Property Get or Set procedure with the same name. Notice that the Property Let procedures have the same names as the Property Get procedures prepared
in the preceding section.

(14)To preserve the Property Get procedure’s local variables between procedure calls, use the following statement format:

  1. Static Property Get FirstName() As String

不知道在不同的类实例之间是否能preserve局部变量,如果这样,我就可以不必维护全局的annatrees变量了。

(15)Before an object can be created, an object variable must be declared in a standard module to store the reference to the object.

  1. Dim emp As New CEmployee

The emp variable will represent a reference to an object of the CEmployee class. When you declare the object variable with the New keyword, VBA creates the object and allocates memory for it. However, the object isn’t instanced until you refer to it in your procedure code by assigning a value to its property or running one of its methods.

  1. Dim emp As CEmployee 
  2. Set emp = New Cemployee 

If you don’t use the New keyword with the Dim statement (as shown above), VBA does not allocate memory for your custom object until your procedure actually needs it.

(16)

  1. Dim emp As New CEmployee 
  2. Dim CEmployee As New Collection

The first declaration statement declares the variable emp as a new instance of the CEmployee class. The second statement declares a custom collection. The CEmployee collection will be used to store all employees’ data. 貌似类名和变量名可以重。

(17)Custom classes recognize only two events: Initialize and Terminate. These events are triggered when an instance of the class is created and destroyed, respectively. Because the statements included inside the Initialize event are the first ones to be executed for the object, before any properties are set or any methods are executed, the Initialize event is a good place to perform initialization of the objects created from the class.

  1. Private Sub Class_Initialize() 
  2.     [code to perform tasks as the object is created goes here] 
  3. End Sub

The Terminate event occurs when all references to an object have been released. This is a good place to perform any necessary cleanup tasks.

  1. Private Sub Class_Terminate() 
  2.     [cleanup code goes here] 
  3. End Sub

To release an object variable from an object, use the following syntax:

  1. Set objectVariable = Nothing

When you set the object variable to Nothing, the Terminate event is generated. Any code in this event is executed then. 其实是要所有对象指针都释放之后,才会触发terminate

(18)Activate the EmpOperations module that you created earlier. The top of the module should contain the following lines:

  1. Option Compare Database 
  2. Option Explicit 
  3. Dim emp As New CEmployee 
  4. Dim CEmployee As New Collection
  5. Sub AddEmployee(empLast As String, empFirst As String, _ 
  6.     empSalary As Currency) 
  7.     With emp 
  8.         .Id = SetEmpId 
  9.         .LastName = empLast 
  10.         .FirstName = empFirst 
  11.         .Salary = CCur(empSalary) 
  12.         If .Salary = 0 Then Exit Sub 
  13.         CEmployee.Add emp 
  14.     End With 
  15. End Sub

When Visual Basic reaches the With emp construct, a new instance of the CEmployee class is created. 模块级变量emp在生成新对象之后被加到集合之中,所以虽然每次调用AddEmployee方法都会使emp指向新的cempolyee对象,但是旧的emp对象并不会因为释放而丢失,因为在集合之中仍有指针指向它,因为它不会被释放。但我并不喜欢这种写法,它维护这样的随用随扔到模块变量并没啥实际意义。我更愿意把这个变量声明写到过程里,作为过程级的临时变量,因为它本来也是临时用途。

(19)

  1. Sub DeleteEmployee(colItem As Integer
  2.     Dim getcount As Integer 
  3.     CEmployee.Remove colItem 
  4. End Sub

The class module procedures were called from the standard module named EmpOperations. This was done to avoid creating a new instance of a user-defined class every time we needed to call it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值