Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 387
miaz Avatar asked Feb 18 '26 05:02

miaz


2 Answers

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);
like image 82
KV Prajapati Avatar answered Feb 20 '26 18:02

KV Prajapati


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

like image 28
BizApps Avatar answered Feb 20 '26 18:02

BizApps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!