如何从 StreamReader 获取数据到新类中

发布于 2024-11-10 03:15:22 字数 1073 浏览 2 评论 0原文

我需要通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然#的苍凉 2024-11-17 03:15:22

您提供的代码中没有接受 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;
    }
}

There is no constructor that takes 2 arguments in the code that you provided.
Your class could look like this:

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;
    }
}
很酷又爱笑 2024-11-17 03:15:22

您的问题是您的类(信息)没有接收 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 的答案。

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:

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;

Optionally, you can change your info class to add a constructor that receive two arguments, like the answer from Alex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文