如何从 StreamReader 获取数据到新类中
我需要通过 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
{
}
}
I need to read in data from a text file, through steamreader, then move that information into another class (I named it info), finally move that into a textbox. I am not sure that I am doing this right at all, am new to it.
The error I get is "work2.info does not contain a constructor that takes 2 arguments"
So here's the code I have to read data in
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;
The class I want to put the data in is
public class info
{
public string newinfo
{
}
public string oldinfo
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您提供的代码中没有接受 2 个参数的构造函数。
你的类可能是这样的:
There is no constructor that takes 2 arguments in the code that you provided.
Your class could look like this:
您的问题是您的类(信息)没有接收 oldinfo 和 newinfo 并填充正确内容的构造函数。两个要更改的选项:
您可以选择更改信息类以添加接收两个参数的构造函数,例如 Alex 的答案。
Your problem is that your class (info), don't have a constructor that receives oldinfo and newinfo and populate the right stuff. Two options to change:
Optionally, you can change your info class to add a constructor that receive two arguments, like the answer from Alex.