On May 30, 2009, at 11:19 AM, Norman Palardy wrote:
On 30-May-09, at 11:34 AM, Michael Diehr wrote:
If a variant holds an object, then Variant.StringValue will
automatically call Operator_Convert for the object (assuming it has
"Operator_Convert() as string" defined).
However, this does not seem to hold true for Operator_Compare when
comparing variants.
Am I doing something wrong or is this just how it goes?
Code:
dim v1 as variant = new cMyClass
dim v2 as variant = new cMyClass
// this calls cMyClass.Operator_Convert
if v1.stringValue = v2.StringValue then ...
// this does NOT call cMyClass.Operator_Copmare
if v1 <> v2 then ...
Class cMyClass
function Operator_Convert() as string
return "foobar"
end function
function Operator_Compare(rhs as cMyClass) as integer
return -1
end function
This makes sense
if v1.stringValue = v2.StringValue then ...
certainly clues the compiler in to which kind of conversion you intend
but
if v1 <> v2 then ...
gives no clue - you might want
if v1.StringValue <> v2.StringValue then ...
or if v1.IntegerValue <> v2.IntegerValue then ...
or if v1.ObjectValue <> v2.ObjectValue then ...
or any others
How would the compiler guess which you intended ?
What, no ESP module? :)
Is there any way to get around this by telling the compiler what I want?
I tried casting to generic objects, but that didn't work, e.g.
if v1.type = variant.TypeObject then
dim o1,o2 as object
o1 = v1.objectValue
o2 = v2.objectValue
if o1 = o2 // still doesn't call the Operator_Compare
The only way I can make it work is to explicitly cast to the object
class, but that's inelegant as it is no longer generic code and
requires a giant Case statement:
if v1.type = variant.TypeObject then
select case v1.objectValue
case isa cClass1
dim o1 as cClass1 = v1
dim o2 as cClass1 = v2
if o1 = o2 // NOW we get the the Operator_Compare() called
case isa cClass2
[...]
case isa cClass3
[...]
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|