0

我无法从以下位置调用字符串“rlist”:

        public void main()
    {
        string rlist;
        if (radioButton1.Checked)
            textBox1.Enabled = false;
        textBox1.ReadOnly = true;
        rlist = "text";
    }

        public void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFile = new OpenFileDialog();
        openFile.Filter = "WTF Files (*.wtf)|*.wtf";
        openFile.Title = "Please Pick your realmlist file:";
        if (openFile.ShowDialog() == DialogResult.Cancel)
            return;
        try
        {
            textBox5.Text = openFile.FileName;
            string file = openFile.FileName;
            TextWriter rlist_writer = new StreamWriter (openFile.FileName);
            rlist_writer.WriteLine(rlist);
            rlist_writer.Close();
        }
        catch (Exception)
        {
            MessageBox.Show("Error opening file", "File Error",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }

我在这一行得到错误:

rlist_writer.WriteLine(rlist);

是否可以从一个函数调用一个字符串并将其发送到另一个函数,该字符串与它最初从中提取的函数中的值相同?

4

2 回答 2

2

听你提问的声音,

您的字符串对于您的主要功能是本地的。因此,根据您的方法名称和对 winforms 的了解(再次假定),您需要使您的字符串类级别

string rlist;
public void main()
{
rlist = "yay"

public void button1_Click(object sender, EventArgs e)
{
someText = rlist;

就目前而言,您无法这样做,因为当您离开该方法时,将通过垃圾收集清理临时(局部)变量

编辑

您不妨也回顾一下

   try
    {
        textBox5.Text = openFile.FileName;
        using(TextWriter rlist_writer = new StreamWriter (openFile.FileName))
        {
            rlist_writer.WriteLine(rlist);
        }
    }
于 2013-04-29T21:58:39.913 回答
0

您可以在类范围内定义该变量,然后如果您在 button_click 事件中调用该变量,它将保持与您的 main 方法中相同的值。

于 2013-04-29T21:59:04.237 回答