如何以与 VB.NET 类似的方式在 VBA 中的类对象模块之间使用比较方法? [英] How to use comparison methods between class object modules in VBA in a similar manner as VB.NET?
问题描述
由于 VBA 中的新项目,我从 VB.NET 迁移过来,老实说,我真的不知道如何处理这里的对象类.我想要达到的是比较不同类对象模块之间的对象.
Due to new project in VBA I moved from VB.NET, to be honest don't really know how to deal between objects classes here. What I want to reach is comparing objects between different class object modules.
例如
班级员工
属性:Name
、Age
要点是:比较两个员工之间的姓名
课程:员工和经理
重点是:比较Employee的Name
和Manager
我知道如何在 VB.NET 中使用,但如何在 VBA 中比较不同类模块对象的属性?
I know how to in VB.NET, but how do I compare properties of different class module objects in VBA?
推荐答案
VBA 不支持 类多态 所以我建议改变你对 的看法Employee
和 Manager
类.
VBA doesn't support class polymorphism so I recommend to change the way you're thinking about the Employee
and Manager
classes.
你不能有一个 Employee
类作为 基类,然后是一个单独的 Manager
类,派生自Employee
.它们可以是2 个独立的 类,实现一个公共接口.
You can't have an Employee
class as a base class and then a separate Manager
class that derives from Employee
. They could be 2 separate classes implementing a common interface.
我会详细说一下.现在让我们通过几个例子...
I will talk about it in detail in a bit. Let's now go through a few examples...
<小时>base
类 (Person
) 和从基类派生的子类.(适用于 C#、VB.NET 等)
A
base
class (Person
) and child classes which derive from the base class. (applies to C#, VB.NET, etc)
但是在 VBA 中你必须这样想:
公开描述位置的枚举属性的基类.
类似的东西
这是让类公开某些属性的最简单方法.它允许您将 Person
对象添加到集合中,并使用简单的 for each
循环和 智能感知!
This is the easiest way to have a class exposing some properties. It allows you to add your Person
objects into a collection and iterate over using the easy for each
loop with Intellisense!
属性比较系统会非常容易
Properties comparison system would be very very easy
注意:同样适用于枚举,因为它隐式转换为数字
两个独立的类,它们都公开公共属性.例如,您有一个 Employee
和 Manager
类,它们都实现了 Person
接口 和一个额外的 Comparer
类暴露一个 Compare()
方法
Two separate classes which both expose public properties. For instance you have an Employee
and Manager
classes which both implement a Person
Interface and an additional Comparer
class exposing a Compare()
method
在您的 VBA 项目中,您需要 4 个类模块和一个标准模块
In your VBA Project you need 4 class modules and a standard module
Person
(这是你的界面)
Public Property Get Name() As String
End Property
Public Property Get Age() As Long
End Property
这个类是Employee
和Manager
都需要实现的接口,以共享一些公共功能(姓名和年龄的getter).拥有接口允许您使用接口类型变量作为枚举器执行 for each 循环.一分钟后你就会看到.
this class is the interface which both the Employee
and Manager
both need to implement to share some common functions (getters for Names and Ages). Having the interface allows you to do the for each loop using the interface type variable as the enumerator. You will see in a minute.
Employee
和 Manager
是相同的.显然,您可以修改它们以适合您的现实生活解决方案.
Implements Person
Private name_ As String
Private age_ As Long
Public Property Get Name() As String
Name = name_
End Property
Public Property Let Name(ByVal Value As String)
name_ = Value
End Property
Public Property Get Age() As Long
Age = age_
End Property
Public Property Let Age(ByVal Value As Long)
age_ = Value
End Property
Private Property Get Person_Name() As String
Person_Name = Name
End Property
Private Property Get Person_Age() As Long
Person_Age = Age
End Property
ComparerCls
您将使用此类的一个实例来比较两个对象的属性或引用.您不一定需要为此开设课程,但我更喜欢那样.
ComparerCls
you will use an instance of this class to compare two objects properties or references. You do not necessarily need to have a class for this but I prefer it that way.
Public Enum ComparisonMethod
Names = 0 ' default
Ages = 1
References = 2
End Enum
' makes names the default comparison method
Public Function Compare(ByRef obj1 As Person, _
ByRef obj2 As Person, _
Optional method As ComparisonMethod = 0) _
As Boolean
Select Case method
Case Ages
Compare = IIf(obj1.Age = obj2.Age, True, False)
Case References
Compare = IIf(obj1 Is obj2, True, False)
Case Else
Compare = IIf(obj1.Name = obj2.Name, True, False)
End Select
End Function
还有你的Module1
代码
And your Module1
code
Option Explicit
Sub Main()
Dim emp As New Employee
emp.Name = "person"
emp.Age = 25
Dim man As New Manager
man.Name = "manager"
man.Age = 25
Dim People As New Collection
People.Add emp
People.Add man
Dim individual As Person
For Each individual In People
Debug.Print TypeName(individual), individual.Name, individual.Age
Next
End Sub
运行 Main()
sub 并在 Immediate Window
以上代码最好的部分是您正在创建Person
接口的引用变量.它允许您遍历集合中实现该接口的所有项目.此外,如果您拥有更多属性和功能,您可以使用 Intellisense,这非常有用.
The best part of the above code is the fact that you are creating a reference variable of the Person
interface. It allows you to loop through all items in the collection that implement the interface. Also, you can use the Intellisense which is great if you have had many more properties and functions.
对比
再看一下ComparerCls
类中的代码
我希望你现在明白为什么我把它分成一个类.它的目的只是照顾比较对象的方式.您可以指定 Enum 顺序并修改 Compare()
方法本身以进行不同的比较.请注意 Optional 参数,它允许您在没有比较方法的情况下调用 Compare 方法.
I hope you see now why I have separated this to be a class. Its purpose is just to take care of the way the objects are being compared. You can specify the Enum order and modify the Compare()
method itself to compare differently. Note the Optional parameter which allows you to call the Compare method without the method of comparison.
现在您可以尝试将不同的参数传递给比较函数.看看结果如何.
Now you can play around passing different parameters to the compare function. See what the results are like.
尝试组合:
emp.Name = "name"
man.Name = "name"
Comparer.Compare(emp, name, Names)
Comparer.Compare(emp, name, References)
Comparer.Compare(emp, emp, References)
<小时>
如果仍有不清楚的地方,请参阅这个关于 VBA 中 Implements
关键字的答案
这篇关于如何以与 VB.NET 类似的方式在 VBA 中的类对象模块之间使用比较方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!