views:

231

answers:

1

Should we use object or variant? What difference does it make?

+4  A: 

If you use Object, then clients must pass in something that supports IUnknown or IDispatch - in short the parameter must be an object. If you use Variant, then clients can pass in anything.

1800 INFORMATION
To add to that: if you choose Variant, you also have to worry about the possibility that the parameter may be 0, Null, Empty, vbNullString, or Nothing ;-)
Mike Spross
@Mike Spross: good comment. As a pedantaholic I must observe that if you make it an Object clients can also pass in Nothing.
MarkJ
@MarkJ: Very true. I was more trying to convey that Variant has a lot more variety when deciding whether or not something exists, whereas Object defines only one way to express the non-existence of something. In particular, the fact that an unassigned Variant defaults to Empty can wreck havoc on code that does IsNull checks on parameters but forgets to do IsEmpty checks. We have a few classes that use Variant properties solely so that the caller can pass Null, but they weren't coded to check for Empty, which caused weird things to happen when the caller didn't explicitly Null the properties.
Mike Spross
I guess my point is, if you go with Variant, you just have to be diligent in how you handle nil values since Variant lets you express nil in more than one way, and if you don't properly check for the different possibilities, your code might go down code paths that it shouldn't go down.
Mike Spross