Q:

C# Passing variable between two event handlers

I have question about passing a variable from one event handler to another. To be specific, here is my code: 
    private void Form1_Load(object sender, EventArgs e)
    {
        string loggedIn = File.ReadLines("Data.txt").Last();

    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        writingToTxtFile();
    }

I would like to pass variable "loggedIn" (or its value) to method "Form1_FormClosing" (to be specific to "writingToTxtFile" function). The only solution I thought of was to use global variables. Is there a way to avoid that? 

A:

You could do this:
private string _loggedIn;

private void Form1_Load(object sender, EventArgs e)
{
    _loggedIn = File.ReadLines("Data.txt").Last();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //do something with _loggedIn
    writingToTxtFile();
}

