VB6对象比较

der*_*ann 5 vb6 object compareto

什么VB6方法允许两个相同类型的自定义对象(在类模块中定义)相互比较?我认为这与Java的compareTo方法相当,但我无法在任何地方找到它.

rav*_*ven 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
Run Code Online (Sandbox Code Playgroud)

如果通过"比较"表示"它们是否在内存中引用相同的对象?",则可以使用Is运算符:

If (object1 Is object2) Then
  Debug.Print "objects references are the same"
Else
  Debug.Print "objects references are NOT the same"
End If
Run Code Online (Sandbox Code Playgroud)


der*_*ann 1

对于其他可能想知道同一问题的人:

经过大量研究后,VB6 似乎没有像 Java 那样的任何类型的内置函数compareToequals方法。

我忘了在Java中,compareTo是在java.lang.Comparable接口中定义的。由于 VB6 中的接口非常糟糕,即使您编写了自己的Comparable接口,您也必须调用对象的Comparable_compareTo方法,除非它被声明为Comparable,这是毫无意义的。

底线:如果您想要VB6 类中的compareTo方法equals,只需将它们放入即可。