Bill,
 
If you have an alias or object variable which may or may not contain a valid object, you can use a test as below before assigning it to an object variable and using it. The trick is to defer the object assignment until you are safely in a function with an error handler.
 
I also use NULL objects where an object variable must be returned. In this case, create it as a small object, outside the bounds of your working tables to ensure no intersects etc.
 
I sometimes find alias variables dont behave as I would expect (see note at function IsObjectT), but its probably because I dont fully understand the scope of their use.
 
 Dim reci as smallint, aobj,atab,arid as alias, oLegit as object, lExists as logical
 if not istableopen("testobjects") then
  open table "f:\temp\testobjects" as testobjects
 end if
 reci = 1
 fetch first from testobjects                        'testobjects has row1 record ok, deleted object; row 2 deleted; row 3 rec, obj ok
  atab = "testobjects"
  aobj = atab + ".obj"
 while not eot(testobjects)

  'oLegit = testobjects.obj
  'lExists = isobject(oLegit)                   'this statement fails with "cant use uninitialised obj var in expr"
  'lExists = isobjectA(aobj)                   'fails with uninit msg
  'lExists = isobject(testobjects.obj)     'works as test but not flexible enough - really need alias var passed
  lExists = isobjectT(str$(atab))   'works as legit object test
  Print "row " + str$(reci) + " object exists: " + str$(lExists)
  if (lExists) then                                 'can now safely manipulate object
           oLegit = testobjects.obj
  End If
  fetch next from testobjects
  arid = atab + ".rowid"
  reci = arid
 wend
'------------------------------------------------------------------------------
Function IsObject (ochk as object) as logical
 Dim o1 as object
 onerror goto errhandler
 o1 = ochk
 IsObject = true
 Exit Function
ErrHandler:
 IsObject = False
End Function
'------------------------------------------------------------------------------
'If this function were to accept the table alias variable
'instead of a string, it fails on the line aobj = atabnm + ".obj"
' in an unexpected manner ("could not convert data")!!!
Function IsObjectT (byval tabnm as String) as logical
 Dim o1 as object, aObj as alias, sobj as string
 onerror goto errhandler
 
 aObj = tabnm + ".obj"
 o1 = aObj
 sobj = str$(o1)    'use obj var in _expression_ tests validity!
 IsObjectT = true    'obj is ok at this point
 
 Exit Function
ErrHandler:
 IsObjectT = False
 ' print "IsObjectT error: " + error$()   dont really want to know
End Function
 
 
 
Phil Waight.
 

_______________________________________ 


Email : [EMAIL PROTECTED]
Web: www.spatialprojects.com.au
Skype: PhilWaight

 
 
----- Original Message -----
From: Bill Thoen
Sent: Saturday, July 29, 2006 1:39 AM
Subject: Re: [MI-L] MAPBASIC Object or not Object?

Spencer Simpson wrote:

>No, I've found that aliases (especially objects) can act strangely if you
>try to extract their values more than once between two successive fetches.
>Maybe newer versions of MapInfo are more robust, but it conditioned me to
>always fetch aliases into variables back in the 1990s.

>
I'd have to say, "Prove it." IMHO, that's a superstition that you
develpoed as you were learning and now it's just a habit. It's similar
to the superstition that using defined constants is not safe.

>Using "degenerate" objects to simulate null objects is useful, but it
>doesn't help fetching them from a table, because you can't guarantee that a
>row didn't have its object deleted.

>
That's right. And for that you can test the object itself (which is
actually an alias). But once you put an object into a variable you need
another way to indicate whether its valid or not.

_______________________________________________
MapInfo-L mailing list
MapInfo-L@lists.directionsmag.com
http://www.directionsmag.com/mailman/listinfo/mapinfo-l
_______________________________________________
MapInfo-L mailing list
MapInfo-L@lists.directionsmag.com
http://www.directionsmag.com/mailman/listinfo/mapinfo-l

Reply via email to