This site requires JavaScript, please enable it in your browser!
Greenfoot back
EIGNERT
EIGNERT wrote ...

2014/3/4

Accessing Variables from Other Classes

EIGNERT EIGNERT

2014/3/4

#
Is there a way to make a variable accessible from all other classes and worlds? I am making a security protocol for an app that I'm making. It requires one to register with a password using one actor, and then another actor asks to enter the password so that they can verify it. I would like to make an if statement which compares these two variable, but since they are defined in different places I cannot seem to do it. This is basically what the if statement looks like at the moment:
if(passwordmatch1 == password)
        {
            Greenfoot.setWorld(new ChipRider_SingleTicketBuy());
        }
        else
        {
            Greenfoot.setWorld(new ChipRider_Retry());
        }
Bare in mind passwordmatch1 and password are both integer variables and they have both been defined in different actors. Also both passwordmatch1 and password were defined in two different if statements. May this cause a problem? Thanks
danpost danpost

2014/3/4

#
Please show the code for these two actors, so we can see how you define them and can figure out what to do.
EIGNERT EIGNERT

2014/3/4

#
The code for password:
public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            String pass = JOptionPane.showInputDialog("Please choose a password (made up of numbers): ");
            int password=Integer.parseInt(pass);
            
            Greenfoot.setWorld(new ChipRider_HomePage2());
        }
    }
And the code for passwordmatch1:
public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            String passmatch1 = JOptionPane.showInputDialog("Please insert your password: ");
            int passwordmatch1=Integer.parseInt(passmatch1);
        }
    }
These are in two different actors.
danpost danpost

2014/3/4

#
Yes. Both fields are being declared within act methods and once the method has finished, those fields are sent to the garbage collector. You first must declare those fields outside of the methods.
EIGNERT EIGNERT

2014/3/4

#
So I would just say
int password = 0;
and
int passwordmatch1 = 0;
before the act methods?
davmac davmac

2014/3/4

#
Yes. Both fields are being declared within act methods
Aha, I'm going to nit-pick here, but technically they are not fields if they are declared within a method - they are local variables.
danpost danpost

2014/3/4

#
EIGNERT wrote...
So I would just say < code omitted > before the act methods?
Yes. You will still have to retain a reference or acquire one to the object that holds the field where the comparison is not taking place in, to access the value of the field for that object. In other words, you still cannot just use 'passwordmatch1 == password' directly.
8bitcarrotjuice 8bitcarrotjuice

2014/3/4

#
... or you could just make the variables static and access them using
ClassName.variableName
? (I know Greenfoot get's messed up when you do this, but just reset the variables in the init() method, or Greenfoot's equivalent, i.e.
public class ClassName {
  public static type variableName = specified choice;
  public void init() {
    variableName = null/0/""/specified choice;
  }
}
?
EIGNERT EIGNERT

2014/3/4

#
Thanks danpost, but how do I retain a reference or acquire one? In this case I want to get 'password' from an actor called PasswordRegister and access it from where 'passwordmatch1' is located, which is an actor called PasswordVerify. Sorry for asking so many question, but I'm very new to Greenfoot.
EIGNERT EIGNERT

2014/3/4

#
Sorry 8bitcarrotjuice but I don't really understand what that means....
8bitcarrotjuice 8bitcarrotjuice

2014/3/4

#
alright, you have int variables, so declare them like so:
public static int passwordmatch1;
instead of
public int passwordmatch1;
Same thing applies for the variable password. After that is done, make the changes to the following code : from
if(passwordmatch1 == password)  
        {  
            Greenfoot.setWorld(new ChipRider_SingleTicketBuy());  
        }  
        else  
        {  
            Greenfoot.setWorld(new ChipRider_Retry());  
        } 
to :
if(Class1.passwordmatch1 == Class2.password)  
        {  
            Greenfoot.setWorld(new ChipRider_SingleTicketBuy());  
        }  
        else  
        {  
            Greenfoot.setWorld(new ChipRider_Retry());  
        } 
Note, change the Class1 to the name of the class where the variable is initiated, and Class2 respectively.
EIGNERT EIGNERT

2014/3/4

#
Thanks so much 9bitcarrotjuice, but I've got one more question. Do I declare them with the static thing within the 'public void act' or outside of it within the green? Thanks for helping me out.
EIGNERT EIGNERT

2014/3/4

#
Meant 8bitcarrotjuice*
You need to login to post a reply.