什么 VB6 方法允许将两个相同类型的自定义对象(在类模块中定义)相互比较?我认为有一个等效于 Java 的 compareTo 方法,但我无法在任何地方找到它。
问问题
2793 次
2 回答
7
如果通过“比较”您的意思是“它们是同一类型吗?”,您可以使用TypeName函数:
If (object1 <> Nothing) and (object2 <> Nothing) then
If (TypeName(object1) = TypeName(object2)) Then
Debug.Print "object types are the same"
Else
Debug.Print "object types are NOT the same"
End If
End If
如果“比较”是指“它们是否引用内存中的同一个对象?”,则可以使用Is运算符:
If (object1 Is object2) Then
Debug.Print "objects references are the same"
Else
Debug.Print "objects references are NOT the same"
End If
于 2010-06-30T13:22:38.897 回答
1
对于其他可能想知道相同问题的人:
在做了很多环顾之后,似乎 VB6 没有像 Java 那样的任何内置compareTo
或equals
方法。
我忘了在Java中,是在接口compareTo
中定义的。java.lang.Comparable
由于 VB6 中的接口非常糟糕,即使您编写了自己的Comparable
接口,也必须调用对象的Comparable_compareTo
方法,除非它被声明为Comparable
,这是没有意义的。
底线:如果您想要VB6 类中的方法compareTo
或equals
方法,只需将它们放入。
于 2010-07-01T01:39:08.163 回答