VB6 Object Comparison
What VB6 method all开发者_运维技巧ows two custom objects of the same type (defined in a class module) to be compared to each other? I think there's an equivalent to Java's compareTo method, but I can't find it anywhere.
If by "compare" you mean "are they the same type?", you can you the TypeName function:
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
If by "compare" you mean "do they reference the same object in memory?", you can use the Is operator:
If (object1 Is object2) Then
Debug.Print "objects references are the same"
Else
Debug.Print "objects references are NOT the same"
End If
For others who may be wondering about the same question:
After doing a lot of looking around, it seems like VB6 doesn't have any kind of built-in compareTo
or equals
methods, like Java does.
I forgot that in Java, compareTo
is defined in the java.lang.Comparable
interface. Since interfaces are so broken in VB6, even if you wrote your own Comparable
interface, you would have to call your object's Comparable_compareTo
method unless it was declared as Comparable
, which is pointless.
Bottom line: if you want compareTo
or equals
methods in your VB6 classes, just put them in.
精彩评论