1

my English isn't good. I have a problem with object specific class. I creating that object using function (WFA: button_click)

    public void button1_Click(object sender, EventArgs e)
    {
        clas = comboBox1.Text;
        name = textBox1.Text;
        sex = radioButton1.Checked;
        if (nazwa.Length != 0 && nazwa.Length != 0)
        {
            Hero Bohater = new Hero(name, clas, sex);
        }
    }

And, when I using another button:

private void chodzenie_Click(object sender, EventArgs e)
        {
            int r = rnd.Next(1, 100);
            if (r % 10 == 0 || r % 10 == 5)
            {
                attack_mode();
            }   
        }
private void attack_mode()
        {
            int wybor = rnd.Next(0, 1);
            if (wybor == 0)
            {
                small_enemy wojo = new small_enemy("Antek");
                while (Bohater.hp >= 0 || wojo.hp >= 0)
                {
                    Bohater.hp -= (wojo.atack - Bohater.defence_p);
                    wojo.hp -= (Bohater.atack_p - wojo.defence);
                }
            }
            else
            {
                big_enemy wojo = new big_enemy("Waldek");
                while (Bohater.hp != 0 || wojo.hp != 0)
                {
                    Bohater.hp -= (wojo.atack - Bohater.defence_p);
                    wojo.hp -= (Bohater.atack_p - wojo.defence);
                }
            }     
        }

Visual can't see my object and receive Error

The name 'Bohater' does not exist in the current context

How to make usable creating object (in function), and uses in global scope?

Ofc all of this is in one file, but another function. I'm try to get Answer in internet, maybe my searching isn't good.

1

2 Answers 2

3

Move Hero bohater outside of button1_Click so that it is defined in the class instead of in the method.

With your current code, bohater is a local variable that can only be used inside button_Click. If you define it in the class instead, bohater becomes a field that can be used by any method in the class.

So, at the top of your class, put:

private Hero bohater;

And inside button_Click, put:

this.bohater = new Hero(name, clas, sex);

You also need to update the attack_mode method, like this:

while (this.bohater.hp >= 0 || wojo.hp >= 0)
{
    this.bohater.hp -= (wojo.atack - this.bohater.defence_p);
    wojo.hp -= (this.bohater.atack_p - wojo.defence);
}

Note that this is used to indicate that bohater is in class scope. (You don't have to use this but it makes the code easier to understand.)

Sign up to request clarification or add additional context in comments.

Comments

1

Define it as the property Bohater in the class you are in:

public Hero Bohater {get; set;}

or define it as a field, if you are just using it internally and temporarily:

Hero Bohater;

instead of defining it inside an if statement inside a button method. This will allow other methods within the class to access it, as well as other classes (if desired). Your definition of the variable only extends to the if statement, and does not exist outside of that statement.

Review how the scope of a property/field/variable is determined. This is just one example of how to solve your problem; there are many others, depending on how you intend to use the "variable".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.