Monday, April 21, 2008

Deep object compare

Ever wanted to check in Flex whether all the public members of an object are the same? You can use the ObjectToString method in that case:

public class SerializeUtil
{
public static function ObjectToString(object: *): String
{
var ba: ByteArray = new ByteArray();
ba.writeObject(object);
return ba.toString();
}
public static function ObjectToByteArray(object: *): ByteArray
{
var ba: ByteArray = new ByteArray();
ba.writeObject(object);
ba.position = 0;
return ba;
}
}

To do the actual comparison:

if (SerializeUtil.ObjectToString(object1)==SerializeUtil.ObjectToString(object2))
{
// Objects are 'equal'
}
else
{
// Objects are not 'equal'
}

6 comments:

Ali said...

Thanks for visiting by my blog (http://aliwriteshere.wordpress.com) and leaving valuable comments. Indeed SL Vs. Flex would be very interesting with Flex having very good armors to beat Microsoft.

I have rich experience mainly in Microsoft platform (both desktop and web) but havent got much hands-on in web 2.0 applications. Silverlight, since its microsoft's baby, obviously seems easy choice for me to start working in. But having read alot about Flex, have made me change my mind and will try Flex also now.

I have noticed you have got very impressive site and profile, will link to you via LinkedIn.

Regards,
Ali
http://aliwriteshere.wordpress.com

T-jo said...

Hi, I like to compare the information within the 2 objects.

Is that possible ?

So if I have 2 objects

newValues.name = "theo"
oldValues.name = "dennis"

I dont want to compare it they both have the item name in it, but I really like to compare the string within name. (and off course not from one item but multiple items within the object)

Ria Flex said...

The proposed solution should take care of this.

Anonymous said...

You can use ObjectUtil.compare() instead.

Greetings, Eugene.

Ria Flex said...

Oops :) Thanks for the suggestion!

Anonymous said...

thanks !! very helpful post!