0

How can I use variables which I defined in Form() function in a button click event?

public Form2(string a, string b)
    {
        int something=4;
        int something=5;
    }

public void hButton_Click(object sender, EventArgs e)
    {

    }

i want to use the variables something and something2 in that gButton_Click event. How can I do that?

2 Answers 2

1
class Form2 {
    int something, something;
    public Form2(string a, string b) {
        something=4;
        something=5;
    }
    public void hButton_Click(object sender, EventArgs e) {
         // example: MessageBox.Show(something.ToString());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can't with the code you have written because the "something" variables only exists inside the scope of the form2() function. If you declare them as class variables then you'll be able to access them inside the button click function. So like this:

class form2 
{
    int something;

    public Form2(string a, string b)
    {
        something=4;
        something=5;
    }

    public void hButton_Click(object sender, EventArgs e)
    {
        //now you can access something in here
    }
}

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.