当前上下文中不存在名称“myReader”
button1是浏览本地文件进行读取,button2是以二进制输出文件内容。为什么不可以?“错误1当前上下文中不存在名称“myReader””privatevoidbut...
button1是浏览本地文件进行读取,button2是以二进制输出文件内容。为什么不可以?
“错误 1 当前上下文中不存在名称“myReader””
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog Dialogfile = new OpenFileDialog();
Dialogfile.Filter = "*.mat|*.dat";
Dialogfile.Multiselect = false;
if (Dialogfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text = Dialogfile.FileName;
String x=" ";
x=Dialogfile.FileName.ToString();
FileStream myStream = new FileStream(x, FileMode.Open, FileAccess.Read);
BinaryReader myReader = new BinaryReader(myStream);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (myReader.PeekChar() != -1)
{
textBox2.Text = Convert.ToString(myReader.ReadInt32());
}
} 展开
“错误 1 当前上下文中不存在名称“myReader””
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog Dialogfile = new OpenFileDialog();
Dialogfile.Filter = "*.mat|*.dat";
Dialogfile.Multiselect = false;
if (Dialogfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text = Dialogfile.FileName;
String x=" ";
x=Dialogfile.FileName.ToString();
FileStream myStream = new FileStream(x, FileMode.Open, FileAccess.Read);
BinaryReader myReader = new BinaryReader(myStream);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (myReader.PeekChar() != -1)
{
textBox2.Text = Convert.ToString(myReader.ReadInt32());
}
} 展开
1个回答
展开全部
因为myReader定义在button1_Click函数中,是局部变量,另一个函数是不可以访问的。
你可以把它移到外面:
BinaryReader myReader;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog Dialogfile = new OpenFileDialog();
Dialogfile.Filter = "*.mat|*.dat";
Dialogfile.Multiselect = false;
if (Dialogfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text = Dialogfile.FileName;
String x=" ";
x=Dialogfile.FileName.ToString();
FileStream myStream = new FileStream(x, FileMode.Open, FileAccess.Read);
myReader = new BinaryReader(myStream);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (myReader.PeekChar() != -1)
{
textBox2.Text = Convert.ToString(myReader.ReadInt32());
}
}
以上为修改好的代码。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询