Test if a class instance exists

If have an array of classes - myClass()
I can update class instances one at a time.
To check that something has actually changed I keep an “old” copy of the instance being updated in oMyClass. This isn’t an array since only one can be changed at a time.
The app includes a method that determines if Save should enable. This includes the line of code

If oMyClass <> nil then
If oMyClass <> myClass(idx) then saveButton.enabled = true
end If

Operator_Compare is implemented in the class definition to run the comparison.

I assumed the Nil test could be used to determine if oMyClass has been created. Instead it seems to invoke Operator_Compare, since the right side is not being passed the Operator_Compare method falls over with a NilObjectException.
The enable save method can be called at other times when oMyClass may not exist … so how do you check, in these circumstances, it a class instance exists?


make sure your operator_compare also handles nil
thats a perfectly valid comparison when the right hand side is nil

function operator_compare(rhs as whatEverTypeYouDefined) as integer
     if rhs is nil then
          return 1
    end if
    if rhs is self then
          return 0
   end if

   // not sure what you want to define for the default 
  // maybe compare actual properties in the instances
  return -1
end function

Ok … thanks for that.
I can do that.


Norman’s advice is solid and you should do that anyway.

To bypass Operator_Compare when testing Nil, I use Is.

if not ( oMyClass is nil ) then ...
// or
if oMyClass is nil then

In code, if I do use <> or = against nil, it’s because I want Operator_Compare to be involved and I’m sure to document that.