Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: how to test for object equality (whether two variables reference the same object)

Tags:

oop

vba

What is the operator or function to test whether two variables of the same custom object type refer to the same object? I've tried

If myObject = yourObject Then

But get a runtime error 438 object doesn't support this property or method. I'm guessing that's telling me to override the '=' operator to test if all the fields of the two objects have the same value. But what I want is to test whether they are the same object.

like image 737
Swiftslide Avatar asked Jun 28 '12 23:06

Swiftslide


People also ask

How do you know if two variables reference the same object?

The is keyword is used to test if two variables refer to the same object. The test returns True if the two objects are the same object. The test returns False if they are not the same object, even if the two objects are 100% equal. Use the == operator to test if two variables are equal.

Which operators can be used to determine whether two variables are referring to the same object or not?

The Is Operator returns True if the objects are identical, and the IsNot Operator returns True if they are not.

How can you compare two objects A and B?

Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity. For example, the expression obj1==obj2 tests the identity, not equality.

Is a function to comparing the object in VB net?

The . Net framework and especially the System. Collection namespace provides us two built in interfaces witch are IComparable and IComparer interfaces in order to compare two objects.


1 Answers

I'm guessing that's telling me to override the '=' operator to test if all the fields of the two objects have the same value.

No, it tells you the objects don't have a default property which would have been called otherwise, and the returned results compared.

You test reference equality with Is

If myObject Is yourObject Then 
like image 188
GSerg Avatar answered Oct 08 '22 01:10

GSerg