【问题标题】:How Do I Use A Variable In One Method If It Was Declared In A Different Method? C#如果变量是在不同的方法中声明的,我如何在一种方法中使用它? C#
【发布时间】:2011-07-10 23:41:53
【问题描述】:

所以我试图找出从以前的方法中“重用”变量的最简单方法,但无法在任何地方准确找到我要查找的内容。

基本上我有一个简单的程序,它使用 openFileDialog 打开一个文本文件(这发生在一个按钮单击中)。在另一个按钮中单击它,将我写的内容写入文件。

我遇到的问题是编写文件,因为我无法重用方法 1 中的路径变量:/

这是我的代码:

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

【问题讨论】:

  • 如果你接受答案就好了。

标签: c# variables methods


【解决方案1】:

使其成为实例变量。

string path = "";

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

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

【讨论】:

    【解决方案2】:

    在您的方法中只需删除声明字符串 filePath 使其看起来像

    filePath = OFD.FileName;
    

    仅此而已

    【讨论】:

    • 您已经声明了与类成员同名的局部变量,因此当您在方法中设置“filePath”时,您只需设置本地“filePath”而不是您的类成员“filePath”。在内部范围(您的 button1_Click 方法)中使用相同类型和名称声明的变量总是将这些从外部范围(在这种情况下是您的类)隐藏起来。
    【解决方案3】:
    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();
        }
    }
    

    【讨论】:

      【解决方案4】:

      你不能在你发布的代码中,因为它超出了范围并且消失了。

      您可以让第一个方法返回选择,然后将其传递给第二个方法。那会起作用的。

      我不喜欢你的方法名称。 button2_Click? button1_Click?两者都没有告诉客户该方法的作用。

      您的方法可能做得太多。我可能有一种方法来选择文件并单独读取和写入。

      【讨论】:

        【解决方案5】:

        button1_Click 中的filePath 字符串声明了string 的一个新实例,它位于成员实例的范围内。删除 string 类型以使方法中的 filePath 引用成员实例。很可能您也不需要 emmeber 实例为 public,但应该是私有的,因为它允许这两种方法进行通信。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-11-27
          • 1970-01-01
          • 2012-03-10
          • 1970-01-01
          • 2012-07-10
          • 2014-03-28
          • 2023-03-11
          相关资源
          最近更新 更多