We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › can anyone explain the This. function
Page Index Toggle Pages: 1
can anyone explain the This. function ? (Read 647 times)
can anyone explain the This. function ?
May 5th, 2010, 9:26am
 
hey, i have checked the processing definition, but do still not understand the this. function, so i was wondering if anyone could explain what the this function is used for ?

a quick example of this is:

this.g = g;
this.u = u;

what is this code doing  ?

and another example

public class hello {
private start start[];
private int count;

public Start( int count ) {
 this.count = count;
 start = new Start[count];
 for ( int h =0; h < count; h++) {
   start[h] = new Start( random( width ), random( height ), random( 10 ));
 }

what is this code doing ?

Many thanks in advnace    Cheesy
Re: can anyone explain the This. function ?
Reply #1 - May 5th, 2010, 9:41am
 
In this (sic) case, it is not a function...

You can find an explanation at this.
In short, it is a reference to the current class instance, the current object.
In the example you show, it is used to distinguish the fields of the class (variables of the class) from the parameter names, since they are identical (by convention).
Some people prefer to decorate either the field names or the parameter names:
public Start(int _count) { count = _count; // ...
or
public Start(int count) { m_count = count; // ...
(m like "member", a naming convention coming from C++...)
for example.
Re: can anyone explain the This. function ?
Reply #2 - May 5th, 2010, 8:10pm
 
I'm glad you answered his question about this.  Wink
Page Index Toggle Pages: 1