Kriptonyc
Kriptonyc

Reputation: 1

How Do I Use A Variable In One Method If It Was Declared In A Different Method? C#

So I'm trying to figure out the easiest way to "reuse" a variable from a previous method, but cant find exactly what I'm looking for anywhere.

Basically I have a simple program that uses openFileDialog to open a text file(this happens in one button click). In another button click it write what I wrote to the file.

the issue I'm Having is writing the file, because I cant reuse the path variable from method 1 :/

Here's my Code:

    public void button1_Click(object sender, EventArgs e)
    {

        OpenFileDialog OFD = new OpenFileDialog();
        OFD.Title = "Choose a Plain Text File";
        OFD.Filter = "Text File | *.txt";
        OFD.ShowDialog();
        string filePath = OFD.FileName;
        if (OFD.FileName != "") {
            using (StreamReader reader = new StreamReader(@filePath))
            {

                while (!reader.EndOfStream)
                {

                    richTextBox1.AppendText(reader.ReadLine());

                }

                reader.Close();
            }
        }
    }

    public string filePath;

    public void button2_Click(object sender, EventArgs e)
    {
        using (StreamWriter writer = new StreamWriter(@filePath)){

            writer.WriteLine(richTextBox1.Text);
            writer.Close();
        }
    }

Upvotes: 0

Views: 230

Answers (5)

Keith
Keith

Reputation: 6834

The filePath string in the button1_Click declares a new instance of a string, in a scope which hies the member instances. Remove the string type to have the filePath in the method refer to the member instance. Most likely you also do not need th emmeber instance to be public, but should be private as it is there is allow the two methods to communicate.

 public void button1_Click(object sender, EventArgs e)
    {
        // etc.
        filePath = OFD.FileName;
    }

 private string filePath;

Upvotes: 0

Microfed
Microfed

Reputation: 2890

public string filePath;

public void button1_Click(object sender, EventArgs e)
{

    OpenFileDialog OFD = new OpenFileDialog();
    OFD.Title = "Choose a Plain Text File";
    OFD.Filter = "Text File | *.txt";
    OFD.ShowDialog();
    filePath = OFD.FileName;
    if (OFD.FileName != "") {
        using (StreamReader reader = new StreamReader(@filePath))
        {

            while (!reader.EndOfStream)
            {

                richTextBox1.AppendText(reader.ReadLine());

            }

            reader.Close();
        }
    }
}

public void button2_Click(object sender, EventArgs e)
{
    // you should test a value of filePath (null, string.Empty)

    using (StreamWriter writer = new StreamWriter(@filePath)){

        writer.WriteLine(richTextBox1.Text);
        writer.Close();
    }
}

Upvotes: 1

duffymo
duffymo

Reputation: 309008

You can't in the code you've posted, because it's out of scope and gone.

You could have the first method return the choice and then pass it to the second method. That would work.

I dislike your method names. button2_Click? button1_Click? Neither one tells clients what the method does.

Your methods might be doing too much. I might have one method to choose the file and separate ones to read and write.

Upvotes: 0

Senad Meškin
Senad Meškin

Reputation: 13756

in your method just remove declaration string filePath make it looks like

filePath = OFD.FileName;

and that is all

Upvotes: 1

Femaref
Femaref

Reputation: 61497

Make it an instance variable.

string path = "";

public void FirstMethod()
{
  path = "something";
}

public void SecondMethod()
{
  doSomething(path);
}

Upvotes: 1

Related Questions