realbasic-nug
[Top] [All Lists]

Re: Operator_Compare with variant objects?

To: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Subject: Re: Operator_Compare with variant objects?
From: Michael Diehr <md03@xochi.com>
Date: Sat, 30 May 2009 11:49:53 -0700
Authentication-results: mx.google.com; spf=neutral (google.com: 74.124.194.228 is neither permitted nor denied by best guess record for domain of realbasic-nug-bounces@lists.realsoftware.com) smtp.mail=realbasic-nug-bounces@lists.realsoftware.com
Delivered-to: listarchive@realsoftware.com
In-reply-to: <3AA0AEC2-D230-4A4C-8788-84C3FCC17A44@great-white-software.com>
References: <AEF09038-DB63-4857-BBE1-AE5D13AA6FF3@xochi.com> <3AA0AEC2-D230-4A4C-8788-84C3FCC17A44@great-white-software.com>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com
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>


<Prev in Thread] Current Thread [Next in Thread>