Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object variable vs. Object reference

Status
Not open for further replies.

foxdbs

Programmer
Aug 5, 2004
66
CZ
It is possible to check, if some variable is Object variable or Object reference?

In other words:
If I make
oObj1 = NEWOBJECT(..)
oObj2 = m.oObj1

then I (later) need some method like
oObj1.IsMaster() && returns .T.
oObj2.IsMaster() && returns .F.
or
IsMaster( m.oObj1 ) && returns .T.
IsMaster( m.oObj2 ) && returns .F.
 
Foxdbs,

No, I don't believe this is possible. The reason is that an object reference and an object variable are really the same thing. Or, to be more precise, an object variable always contains an object reference (in much the same way that a numeric variable contains a number).

I'm not clear why you would want to do this. I've never come across this requirement before.

Mike

Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Hi, foxdbs!
oObj1 = NEWOBJECT(..)
oObj2 = m.oObj1
First of all, the first sentence returns hash for newly created object, that can be checked via TYPE() or VARTYPE
and if You had have created obejct these functions both return "O". So You have object reference.

Prefix m. only for making distinguish between memory variables and table fields.
Try mentioned functions for checking type of your m.oObj1

BR


Juri Shutenko
Municipality of Maardu, Estonia
 
Thanks. I understand it now.

-> MikeLewis

I asked this because I want find some solution in the second question, which I have started as a standalone thread (how to know if the released object reference is the last one)
- and I cannot find solution yet, so my idea was to go through the other way.
 
Foxdbs,

Yes, I saw that other thread, but wasn't sure how to answer it. The only thing I can add is that this isn't really a problem. VFP itself keeps count of the number of times an object is referenced, and knows to clean up after the last reference is released.

It's true that you could possibly see "dangling" references, but this shouldn't happen if you are always careful to release every variable when you have finished with it. Usually, the easiest way of doing that is simply to let the variable go out of scope.

Mike

Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Assuming your version of VFP is sufficiently recent, try this:

x=Newobject('custom')
y=Newobject('custom')
z=x && reference instead of a new object

?compobj(x,y)
?compobj(x,z)
?x=y
?z=x

 
Yes, now I see, that in VFP9 the first expression compobj(x,y) returns True!

In VFP7 debugger I see exactly same properties of both m.x and m.y objects. So it looks like a bug of COMPOBJ function.

Do you think there can be something in the VFP7 engine, what improperly removes object references (f.e. when variable or object property (of type: object) goes out of scope)?
 
Hi

I am using VFP8
Code:
oObject = NEWOBJECT('custom')

oObj1 = oObject

IF VARTYPE(oObj1) = 'O'
   =MESSAGEBOX('I found Object1')
ELSE
   =MESSAGEBOX('I cannot find Object1')
ENDIF


IF VARTYPE(oObj2) = 'O'
   =MESSAGEBOX('I found Object2')
ELSE
   =MESSAGEBOX('I cannot find Object2')
ENDIF

oObj2 = oObject

IF VARTYPE(oObj2) = 'O'
   =MESSAGEBOX('Now I found Object2')
ELSE
   =MESSAGEBOX('Oh no ! Still I cannot find Object2')
ENDIF

RELEASE oObject
IF VARTYPE(oObject) = 'O'
   =MESSAGEBOX('Still I find oObject')
ELSE
   =MESSAGEBOX('oObject is released.')
ENDIF
IF VARTYPE(oObj1) = 'O'
   =MESSAGEBOX('I can find Object1 after releasing oObject')
ELSE
   =MESSAGEBOX('I cannot find Object1')
ENDIF
IF VARTYPE(oObj2) = 'O'
   =MESSAGEBOX('Still I find Object2')
ELSE
   =MESSAGEBOX('Nope ! I cannot find Object2')
ENDIF

____________________________________________
ramani - (Subramanian.G) :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top