How do I recall a string from a different function?

advertisements

I am having trouble calling the string "rlist" from:

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

to

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

I get the error on this line:

rlist_writer.WriteLine(rlist);

is it possible to call a string from one function and send it to the other with the same value it had in the function it was originally pulled from?


By the sounds of your question,

Your string is local to your main function. So judging by your method names and knowledge of winforms(presumed again) you need to make your string class level

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

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

As it currently stands you are not able to, as temporary (local) variables will be cleaned through garbage collection when you leave the method

Edit

You may wish to review this also

   try
    {
        textBox5.Text = openFile.FileName;
        using(TextWriter rlist_writer = new StreamWriter (openFile.FileName))
        {
            rlist_writer.WriteLine(rlist);
        }
    }