Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Dec 11, 2018 10:14 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Trying to understand the self and me keywords.
PostPosted: Thu Oct 06, 2005 8:15 pm 
Offline
User avatar

Joined: Tue Oct 04, 2005 12:06 pm
Posts: 27
Location: Hans Island, Canada
RB has two keywords self and me that seem very similar.

From the "RB user's guide":
Quote:
"Me refers to the control that you are working with."
"The pronoun Self refers to the control’s parent window."
"The Self function returns a reference to the instance of a window (or class) that calls the Self function."

To a newbie the last statement might be seen to not agree with the first two. If I had a window inside a window (or a widget inside a widget) and called self what would I get?

Then in the "Language Reference":
Quote:
"Me refers to the control that fired the current event. Outside an event handler, it simply refers to the current object (Self)."
"Me is only different from Self inside an event handler for a control on a window."

So, here we have some more clarification. Inside event handlers self means "parent", otherwise self means the same as me.

Also from "REALbasic: The Definitive Guide, 2nd Edition"
Quote:
"Code can obtain a reference to the instance that is actually running that code by way of the Self function."
"Code within a control's event handlers in a window class's Code Editor can obtain a reference to the control instance that is actually running that code by way of the Me function (not to be confused with the Self function, which refers, in such code, to the containing window)."


So, as far as I can tell, using me will always return the "this pointer equivalent", a handle to the instance. Using self is context dependent, so if I stick to only using it when I want to get at the parent widget, I should be safer. Is this correct?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Oct 06, 2005 9:34 pm 
Offline

Joined: Fri Sep 30, 2005 9:30 pm
Posts: 1712
Location: U.S.A.
If you have code inside a button's action handler:
me.caption   //Refers to the button

self.title //Refers to the window that holds the button


I have never used self outside an event handler, so I can't tell you what the difference is.

When in doubt you can use the full name instead of me or self. For example, instead of saying me.caption, say pushButton1.caption. That should leave no doubt what you intend.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Oct 07, 2005 12:49 am 
Offline

Joined: Wed Sep 28, 2005 6:06 pm
Posts: 292
Quote:
Using self is context dependent, so if I stick to only using it when I want to get at the parent widget, I should be safer. Is this correct?

It's actually the other way 'round; the documentation is not very clear on this subject. 'Self' is not context dependent: it always refers to the current object. 'Me' usually refers to the current object, but if you are inside an event handler for a control on a window, 'Me' refers to the control, not the window.

Here's another way of thinking about it: 'Self' always refers to the object whose code editor you are in. Look at the name on the editor tab (or the editor window, if you're using an old version): that's 'Self'.

You only need to worry about it if you are inside an event handler for a control on a window; in all other cases, 'Self' and 'Me' are synonyms.

Mars Saxman
REAL Software


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Oct 07, 2005 1:24 am 
Offline
User avatar

Joined: Fri Sep 30, 2005 3:53 pm
Posts: 3261
Location: England
Mars Saxman wrote:
Here's another way of thinking about it: 'Self' always refers to the object whose code editor you are in. Look at the name on the editor tab (or the editor window, if you're using an old version): that's 'Self'.

Thanks for that simplification, Mars.

It's one of those silly conceptual things. Because I usually get at the event handler by double-clicking the visual representation of the control, it feels like I'm working in the control.

Until now, whenever this discussion has come up, I've had to remind myself that the control is raising an event and the window is handling it, so of course the event handler belongs to the window.

_________________
Steve Garman
Using REALbasic 2008r2 Professional on Windows Vista Ultimate
and REALbasic 2009r5.1 Professional on Linux Ubuntu 9.04 Desktop
and REAL Studio 2010r1 Personal on Linux Ubuntu 9.10 Desktop
Occasional blog


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Oct 07, 2005 4:03 pm 
Offline
User avatar

Joined: Tue Oct 04, 2005 12:06 pm
Posts: 27
Location: Hans Island, Canada
Thanks for the replies.

Quote:
"'Self' always refers to the object whose code editor you are in."


This is a much better way of looking at it for me. And then it really is consistent.

So, if I decide to subclass a button and handle its events from its own tab in the IDE, then self refers to the button. If I use a generic button and simply handle the events in the window, where I place the button, then self refers to the window.

This confusion arose for me due to thinking about the possibility of copying/moving code from one of these handlers to another and whether I would need to change self to me etc.

I am sure the REALbasic name resolution/scoping rules will trip me up somewhere, so I think I might try to stick with explicitly scoping things up front inside class member functions.

I think I might need to do more coding and less reading at this point. Thanks again.
--
RB2005r3 - WinXPPro


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Oct 07, 2005 8:03 pm 
Offline
User avatar

Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN
I happened to blog on this topic a long while ago. In case anyone's interested:

Me/Self and When you Should Use Them


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Oct 09, 2005 6:33 pm 
Offline
User avatar

Joined: Tue Oct 04, 2005 12:06 pm
Posts: 27
Location: Hans Island, Canada
Thanks for that Aaron. I wish that had shown up in my earlier web searches.

I think the RB bashing follow-up comment to your blog is unwarranted, in this case. The real issue here was simply the tack chosen by the documentation/book and the somewhat poor choice of keyword/parameter name for me.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group