我无法调用字符串" 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);
是否可以从一个函数调用一个字符串并将其发送到另一个函数,并使用与最初从中拉出的函数相同的值?
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
您可以在类范围中定义该变量,然后如果在button_click事件中调用该变量,它将保持与main方法中相同的值。