user3828453
user3828453

Reputation: 127

Variable Scoping in c#.net

Hi it might sound a simple task but i am a bit confused here

I have an event :

private void ManReg_Click(object sender, EventArgs e)
{
 int store = data[0];
}

and then another event function like :

private void measurementsRead_Click(object sender, EventArgs e)
{
}

I would like to use the variable "store" in the second function. How can I ?

The form is declared as follows :

namespace myRfid
{
    public partial class Form1 : Form
    {
        }
}

Upvotes: 1

Views: 321

Answers (2)

Neel
Neel

Reputation: 11741

You can set it as shown in below code in your class but private as it would be used within class only as shown below :-

private int store;

private void ManReg_Click(object sender, EventArgs e)
{
    this.store = data[0];
}

private void measurementsRead_Click(object sender, EventArgs e)
{
     this.store//// however you want to use
}

Upvotes: 1

Pavel Krymets
Pavel Krymets

Reputation: 6293

You should put variable onto the class level

class ...
{
    private int store;

    private void ManReg_Click(object sender, EventArgs e)
    {
        store = data[0];
    }

    private void measurementsRead_Click(object sender, EventArgs e)
    {
        // use store
    }
}

Upvotes: 1

Related Questions