我需要从文本文件中读取数据,通过steamreader,然后将该信息移动到另一个类(我将其命名为info),最后将其移动到文本框中。我不确定我是否正确这样做,我是新手。 我得到的错误是“work2.info不包含带有2个参数的构造函数” 所以这是我必须在
中读取数据的代码 private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "text files (*.txt)|*txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
StreamReader data = new StreamReader(myStream);
string newinfo = data.ReadLine();
string oldinfo = data.ReadLine();
info pepinfo = new info(newinfo, oldinfo);
pepinfo.newinfo = textBox1.Text;
pepinfo.oldinfo = textBox2.Text;
我要将数据输入的类是
public class info
{
public string newinfo
{
}
public string oldinfo
{
}
}
答案 0 :(得分:2)
没有构造函数在您提供的代码中包含2个参数 你的课可能是这样的:
public class Info
{
public string NewInfo { get; private set; }
public string OldInfo { get; private set; }
public Info(string newInfo, string oldInfo)
{
NewInfo = newInfo;
OldInfo = oldInfo;
}
}
答案 1 :(得分:0)
你的问题是你的类(info),没有接收oldinfo和newinfo的构造函数并填充正确的东西。有两个要改变的选择:
private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "text files (*.txt)|*txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
StreamReader data = new StreamReader(myStream);
string newinfo = data.ReadLine();
string oldinfo = data.ReadLine();
info pepinfo = new info();
pepinfo.newinfo = newinfo;
pepinfo.oldinfo = oldinfo;
或者,你可以改变你的信息类来添加一个接收两个参数的构造函数,比如Alex的答案。