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 › Returning a value from an object
Page Index Toggle Pages: 1
Returning a value from an object (Read 526 times)
Returning a value from an object
Jan 27th, 2010, 1:15pm
 
I am trying to return a value from an object.   I have the follwoing code snippets but I cant work out what I am doing wrong.  I dont find any examples in the processing reference documentation but I do find examples in the referenced java tutorial.

I have defined an object class thus:

class atom {
 public float lat,lon;
 
 <snip>

public float ypos() {
   return lat;
 }
   public float xpos() {
   return lon;
 }
}


I am now attempting to call it in the following manner:

import etc

atom[] atoms;
int ATOMS=255;


void setup() {

}


void draw() {

<snip>
 for (int i=0;i<ATOMS;i++) {
//  this bit works fine
   atoms[i].move();
   atoms[i].draw();


 }

// this bit fails....

   println (atoms[0].ypos, atoms[0].xpos);

}

I get the error "ypos cannot be resolved or is not a field"

What am I doing wrong?
Re: Returning a value from an object
Reply #1 - Jan 27th, 2010, 1:24pm
 
xpos and ypos aren't fields. they are methods.

you need:
println (atoms[0].ypos(), atoms[0].xpos());

(alternatively you can atoms[0].lat and atoms[0].lon as your members are public. using methods (and having the members as private) is the more oop way of doing things though even if it is more typing.)
Re: Returning a value from an object
Reply #2 - Jan 28th, 2010, 12:17am
 
Koogy,  thanks for reply.  Having slept in it I realised my mistake.

Thx
Page Index Toggle Pages: 1