Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

undefined variable!

 
Fahed Ghazali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there...
here is some of my code:

when I compile this... it says that the Variable "Width" in my method "slowShow()" is undefined.
how do I get around solving this? I cant define width as an instance variable as it comes into play only after the constructo uses the argument int[][] pixels.
pls help me out yall!
cheers

[This message has been edited by Fahed Ghazali (edited May 13, 2001).]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you just define width as an instance variable of the class i.e. int width;
then in the constructor initialize it to it's appropriate value
i.e width = initialValue;

Bosun
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bosun is correct, the problem is that you have declared and initialized the width (and height) variable inside your constructor, and it is therefore local to that constructor. Once it is over the rest of the code can't see it.
To prevent this, declare the variable outside the constructor, and just initialized it in the constructor. This way the methods will be able to access it later.
You are also going to have a problem with you GrayScaleCanvas because you declared it as an instance variable and then REDECLARED it in the constructor - which created a DIFFERENT variable with the same name but local to the method. When you initialize gsc do NOT redeclare it.

[This message has been edited by Cindy Glass (edited May 13, 2001).]
 
Fahed Ghazali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx... appreciate the help guys!!
now another slight problem!
in my original code right... I have also initialised the argument int[][]pixels in the constructor... coz thass where Im gonna get the information from. But I need to use the 2d array pixels an all my other methods. How do I declare this as an instance variable?
do I just add
int[][]pixels as an instance variable n then in the constructor what do I do to make it equal to the int[][]pixels in the argument??

this might seem trivial to yall...... but Im still learning ppl!
cheers
thanx
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put
this.pixels = pixels ;
in the constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic