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 & HelpPrograms › can't access variable outside of class
Page Index Toggle Pages: 1
can't access variable outside of class (Read 432 times)
can't access variable outside of class
Jun 18th, 2008, 12:10am
 
Hi All,

The code I experience some troubles with is at: http://benmeijering.nl/applet/

If you uncomment "println(number.length);" in Player.hit() I get the error message: The type of this expression, "int", is not a reference type.

The code itself is yet meaningless; eventually I want to do some hit tests... The problem, though, I can't understand, because I can't seem to replicate this problem with variables, and can't even replicate this problem in simpler programs with the same classes.

Hope someone sees the problem.

Thnx, ben
Re: can't access variable outside of class
Reply #1 - Jun 18th, 2008, 12:35am
 
You have 2 variables called number, there's a global array of Numbers called number, and within the Player class there's a int called number.

So when you access number.length within Player, it tries to get a value called "length" from the int, which is impossible.
Re: can't access variable outside of class
Reply #2 - Jun 18th, 2008, 12:58am
 
John, thanks.
Indeed, that's the problem. So'll come up with another variable name.
I don't want to be lazy but is there a way to access the array 'number' outside the class Player and not change the the variable name 'number' within player; at some point I'll run out of synonyms....
Re: can't access variable outside of class
Reply #3 - Jun 18th, 2008, 10:07am
 
no. and that would be a terrible idea.

you can use numbers and number, if you need some inspiration ;-)
Re: can't access variable outside of class
Reply #4 - Jun 18th, 2008, 10:42am
 
I had that coming, I guess.
But I thought there could be some syntax to refer to variables outside classes, as well as, variables inside classes (this).
Re: can't access variable outside of class
Reply #5 - Jun 18th, 2008, 10:53am
 
this. only helps distinguish between passed in arguments, and member variables.

e.g.
Code:
int number;

class foo
{
float number;
void thing(String number)
{
//number == String
//this.number == float
//no way to get at the int number.
}
}
Page Index Toggle Pages: 1