6

What VB6 method allows 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.

derekerdmann
  • 17,696
  • 11
  • 76
  • 110
  • "two objects defined in a class module" -- What do you mean by "defined "? You can only define one class per class module. Perhaps you meant "two objects instantiated" but what would be the relevance of where they were instantiated? – onedaywhen Jun 30 '10 at 13:41
  • I'm working with two objects, both of which are instances of a single class that I defined in a class module. Sorry that was unclear. – derekerdmann Jun 30 '10 at 13:44
  • OK I think I see now. I'm almost sure there's nothing build into VB. Out of interest, what would you expect to happen if one of the attributes was an object -- would it need to call *that* object's compareTo method...? – onedaywhen Jun 30 '10 at 14:00
  • @onedaywhen Yes, it would. Depending on the criteria you're using to compare objects, there could be several recursive calls involved. – derekerdmann Jun 30 '10 at 14:04

2 Answers2

7

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
raven
  • 18,004
  • 16
  • 81
  • 112
  • What I mean is that two instances of a class with identical attributes should return true. Naturally, you could compare those attributes manually, but I thought that VB6 had a method that you could define where that would happen automatically. – derekerdmann Jun 30 '10 at 13:40
  • 1
    Careful: object1 and object2 could be of different types but currently resolve to Nothing. – onedaywhen Jun 30 '10 at 13:43
  • @onedaywhen: Good catch. I assumed that TypeName returned the type of the object variable even if it was set to "Nothing", but instead it returns "Nothing". That complicates things. I put a check in my example code, but if one of those objects is Nothing, I guess a type comparison isn't possible. – raven Jun 30 '10 at 14:55
  • @derek: After reading the clarification you made in your comments, I now understand that I haven't answered your question. Sorry, I know of no method in VB6 that can do what you wish. – raven Jun 30 '10 at 15:05
  • Hey, don't sweat it. Thanks anyway! – derekerdmann Jun 30 '10 at 15:39
  • +1 There's no way to compare object instances by value. I don't think you can even compare structure instances. – MarkJ Jun 30 '10 at 16:37
1

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.

derekerdmann
  • 17,696
  • 11
  • 76
  • 110