Article ID: | qOOP003 |
Date Revised: | July 13, 1997 |
Keywords: | this, C++, self |
Question: How is the THIS object reference used?
Answer: "this" is a generic reference to the object itself. "this" only has meaning within a method of the object. It gives the method code access to the object properties and methods without knowing what the object's real name is.
So for example, in a form's Init() method:
this.Caption = "A New Caption"
this.Top = 100
would change the titlebar caption and move the window top to 100 pixels from the top of the main VFP window. If you are familiar with C++ you'd just:
Caption = "A new Caption";
Top = 100;
But in VFP that code would actually create two new private memory variables that would just disappear, go out of scope when the method ended.
Every member function gets a this pointer as the first argument to the function. Since C++ uses a much more controlled namespace environment it knows you are talking about the member variables. You can also explicitly use the C++ this pointer like:
this->Caption = "A new caption";
this->Top = 100;
Smalltalk uses self in the same fashion
In VFP we also have a thisform reference which lets form objects communicate with whatever form they happen to be on, no matter how deep their containership has them.