Using variable across methods
Hi, I'm looking to use the variable flagDimensions in method norwayFlag after defining it as shown. However, it's telling me that the variable is undefined. Please ignore the UI. etc, it's part of a custom library used by the learning institution. Please also note that this is not the whole code, just the relevant piece.
So, what have I done wrong?! Thanks!
Code :
public void flagDimensions(double x, double y, double flagHt, double flagLgt){
x = 100;
y = 100;
flagLgt = (flagHt/2)*3;
}
/** The flag for Norway is a red rectangle with
* a blue cross with a white border, slightly off-set to the left-hand side;
*/
public void norwayFlag(){
UI.initialise(); // not necessary, but avoids some problems when debugging
UI.setColor(Color.red);
UI.fillRect(flagDimensions);
UI.setColor(Color.white);
UI.fillRect(x-((flagHt/5)*2), y, (flagHt/5), flagLgt);
}
Re: Using variable across methods
Quote:
I'm looking to use the variable flagDimensions in method norwayFlag after defining it as shown. However, it's telling me that the variable is undefined.
Yes the variable flagDimensions is undefined. What you defined was a method flagDimensions().
Re: Using variable across methods
Oh, ok, my mistake. Can you please inform me of how to correct said mistake, and direct my program along the proper path?
Re: Using variable across methods
It's a bit difficult to say *how* you should do whatever it is you want to do without knowing first what it *is* that you want to do.
Currently the flagDimensions() method does nothing at all. It assigns some values to the parameter variables, but these variables last only as long as the method does. Ie they will "go out of scope" - disappear without trace - after the method finishes, so assigning anything to them is without any lasting effect.
As far as the fillRect() method is concerned, look in the documentation for that class and see what sort of arguments it expects you to pass it. (The number and type of those arguments)
Re: Using variable across methods
Thank you for your assistance.
Re: Using variable across methods