开发者

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

开发者 https://www.devze.com 2023-03-19 10:45 出处:网络
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.

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


Make it an instance variable.

string path = "";

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

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


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

filePath = OFD.FileName;

and that is all


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


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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消