miaz
miaz

Reputation: 45

Changing an Object's properties in another class?

I'm trying to change an Object's properties from another class, like so.

abilities.cs (class)

        public static void hideAllButtons()
        {
            frmFight fight = new frmFight();
            fight.btnAbility1.Visible = false;
            fight.btnAbility2.Visible = false;
            fight.btnAbility3.Visible = false;
            fight.btnAbility4.Visible = false;
            fight.btnAbility5.Visible = false;
            fight.btnAbility6.Visible = false;
            vars.buttonsVisible = false;
        }

I'm trying to use the method from the previous class to change the object's properties in the following Form;

frmFight.cs (form)

        private void btnAbility1_Click(object sender, EventArgs e)
        {
            abilities.hideAllButtons();
            btnAbilities.Enabled = false;
        }

I've tried everything in my knowledge and understanding, and a lot of looking up on the internet. I've tried making the objects static, public, creating the object within the class. But nothing works. Usually I get StackOverFlow errors.

I'm pretty new to OOP, too, but I'm not an idiot so don't think to go too 'lightly' on me with a possible fix, or cause, of my problem - if you understand, that is.

Upvotes: 3

Views: 3881

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94645

Method hideAllButtons is static so should have a Form argument.

 public static void hideAllButtons(frmFight fight)
 {
  fight.btnAbility1.Visible = false;
  fight.btnAbility2.Visible = false;
  fight.btnAbility3.Visible = false;
  fight.btnAbility4.Visible = false;
  fight.btnAbility5.Visible = false;
  fight.btnAbility6.Visible = false;
  //vars.buttonsVisible = false; // What about this???
 }

and call this method in click handler,

hideAllButtons(this);

Upvotes: 4

BizApps
BizApps

Reputation: 6130

This really wont work because your controls is on your frmFight.

You need to do is place

 private void hideAllButtons()
    {
        btnAbility1.Visible = false;
        btnAbility2.Visible = false;
        btnAbility3.Visible = false;
        btnAbility4.Visible = false;
        btnAbility5.Visible = false;
        btnAbility6.Visible = false;
        vars.buttonsVisible = false;
    }

on your frmFight.cs (form) as private method and just call it on your button click to make it simpler.

    private void btnAbility1_Click(object sender, EventArgs e)
    {
        hideAllButtons();
        btnAbilities.Enabled = false;
    }

Regards

Upvotes: 2

Related Questions