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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using THIS as parameter

Status
Not open for further replies.

sebes

Programmer
Apr 5, 2011
45
RO
Hello ,

I'm looking at a program written by someone else...

In the program, a specific function is called like this:
...
showdata(this)
...

Down the road...
procedure showdata(pParm1, pParm2)
...
...
return

In this procedure I can see the values of pParm1(an object), pParm2(a logical value) in the debugger BUT I don't understand how they relate to THIS from the calling statement.

Thanks for your help.

-sebes


 
THIS is an allowed object reference. If the caller passes THIS, this arrives at the callee as a passed in parameter like any other object or value. That is normal behaviour and the way to pass on a reference.

It should not be overused, as you can break encapsulation by this, but indeed it's helpful to centrelaize some functionality for several objects into a function, which is not limited to a certain class of objects.

For exaple a very normal usage of this kind of passing references is, if a form adds itself to an event handler object.

Bye, Olaf.

 
Sebes,

Basically, what's happening here is that ShowData is receiving, in its first parameter, an object reference to the object that contains the calling code. So, if the calling code is in the Init of a form, THIS will be a reference to the form. If the code is in, say, the Click of a button, THIS will refer to the button, and so on.

ShowData can now access the properties and methods of the calling object. For example, if THIS refers to the form, and ShowData wants to know the Caption of the form, it just need to refer to THIS.Caption.

The fact that ShowData is receiving two parameters means that the second one is optional. If the caller passes a second parameter, ShowData will receive it. Otherwise, it will receive a logical .F. (which is a sort of "empty" parameter).

Does that help at all?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
That makes sense. Thanks all.

-Sebes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top