Saliha Uzel
Saliha Uzel

Reputation: 151

C# Windows Application-Pass Variable to Button Click Function

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?

Upvotes: 0

Views: 2150

Answers (2)

Mansfield
Mansfield

Reputation: 15150

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
    }
}

Upvotes: 0

FiftiN
FiftiN

Reputation: 797

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());
    }
}

Upvotes: 1

Related Questions