如何在VBA中类对象模块之间使用比较方法与VB.NET类似的方式? [英] How to use comparison methods between class object modules in VBA in a similar manner as VB.NET?

查看:377
本文介绍了如何在VBA中类对象模块之间使用比较方法与VB.NET类似的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于VBA中的新项目我从VB.NET移动,说实话,真的不知道如何处理对象类之间在这里。我想要达到的是比较不同类对象模块之间的对象。



例如





     属性:姓名年龄

     点是:比较两个员工之间的姓名 s



类:员工经理

点是:compare 名称员工经理

姓名



如何在VB.NET中,但是如何比较VBA中不同类模块对象的属性?

解决方案

支持类多态性,因此我建议您更改 Employee <$



您不能拥有 员工 / code> 类作为基类,然后单独的 Manager strong>派生从 Employee 。它们可以是实现公共接口的 2个独立类。



我会详细讨论一下。现在我们来看几个例子...






↓轻松方法↓





A 基础类别 Person / em>)和从基类派生的子类。 (适用于C#,VB.NET等)



,但在VBA中你必须想到它


$ b

一个基类, $ b

类似





>



这是让类暴露一些属性的最简单方法。它允许您将 Person 对象添加到集合中,并使用easy code> 循环使用 Intellisense





属性比较系统非常简单



>



>






↓更复杂的方法↓






两个单独的类,它们公开了公共属性。例如,您有 Employee Manager 都会实施 Person 接口和另一个 Comparer 类展示 比较() 方法





/ p>

>



(这是您的介面) b

 公共属性Get Name()As String 
结束属性

公共属性Get Age()As Long
End Property

此类是 Employee 经理都需要实现以共享一些常见的功能(名称和年龄的getter)。拥有该接口,您可以使用接口类型变量作为枚举器为每个循环执行。

> 经理
是相同的。

b $ b私人名称_作为字符串
私人年龄作为长期

公共财产获取名称()作为字符串
名称=名称
结束财产

公共财产让名字(ByVal价值作为字符串)
name_ =价值
结束财产

公共财产获取年龄()长期
年龄= age_
结束属性

公共属性让年龄(ByVal价值长)
age_ =价值
结束属性

私有属性获取Person_Name String
Person_Name = Name
结束属性

私有属性Get Person_Age()As Long
Person_Age = Age
结束属性

ComparerCls 该类的实例比较两个对象属性或引用

 公共枚举比较方法
名称= 0'default
Ages = 1
引用= 2
结束Enum

'使名称成为默认的比较方法
公共函数比较(ByRef obj1 As Person,_
ByRef obj2 As Person,_
可选方法As ComparisonMethod = 0)_
作为布尔

选择案例方法
案例年龄
比较= IIf(obj1.Age = obj2.Age,True,False)
案例参考
比较= IIf(obj1是obj2,True,False)
案例Else
比较= IIf(obj1.Name = obj2.Name,True,False)
结束选择

结束函数

您的 Module1 代码

 选项显式

Sub Main()

Dim emp As New Employee
emp.Name =person
emp.Age = 25

Dim man As新经理
man.Name =manager
man.Age = 25

新集合
People.Add emp
People.Add人

个人为个人
个人在人中
Debug.Print TypeName(个人) ,individual.Name,individual.Age
Next

End Sub

运行 Main() sub并检查立即窗口



>



上面代码的最好的部分是你创建 Person 接口的引用变量。它允许您循环访问实现接口的集合中的所有项目。此外,您可以使用Intellisense,如果您有更多的属性和功能,这是伟大的。






比较






再次查看 ComparerCls class





我希望你现在看到为什么我把它分离成一个类。它的目的只是照顾对象的比较方式。您可以指定枚举顺序并修改 Compare()方法本身来进行不同的比较。注意可选参数,它允许您调用Compare方法而不使用比较方法。



>



现在你可以通过不同的参数传递给比较函数。查看结果



尝试组合

  emp.Name =name
man.Name =name

Comparer.Compare(emp,name,Names)
Comparer.Compare(emp,name,References)

Comparer.Compare(emp,emp,References)






如果还不清楚,请参阅 此答案关于VBA中的实施关键字


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.

e.g

class Employee
     properties: Name, Age
     point is: compare Names between two Employees

classes: Employee and Manager
point is: compare Name from Employee with Name of Manager

I know how to in VB.NET, but how do I compare properties of different class module objects in VBA?

解决方案

VBA doesn't support class polymorphism so I recommend to change the way you're thinking about the Employee and Manager classes.

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...


↓ Easy approach ↓


A base class (Person) and child classes which derive from the base class. (applies to C#, VB.NET, etc)

but in VBA you have to think of it like this:

A base class which exposes an enum property describing the position.

Something like

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

note: same applies to enum as its implicitly converted to a number


↓ More complex approach ↓


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

In your VBA Project you need 4 class modules and a standard module

Person (this is your Interface)

Public Property Get Name() As String
End Property

Public Property Get Age() As Long
End Property

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 and Manager are identical. Obviously you may modify them to suit your real life solution.

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 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

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

run the Main() sub and check out the results in the Immediate Window

The best part of the above code is the fact that you creating a reference variable of the Person interface. It allows you to loop through all items in collection which implement the interface. Also, you can use the Intellisense which is great if you have had many more properties and functions.


Comparison


Take a look again at the code in the ComparerCls class

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

try combinations

emp.Name = "name"
man.Name = "name"

Comparer.Compare(emp, name, Names)
Comparer.Compare(emp, name, References)

Comparer.Compare(emp, emp, References)


If something is still unclear refer to this answer about the Implements keyword in VBA

这篇关于如何在VBA中类对象模块之间使用比较方法与VB.NET类似的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆