从二进制文件中读取列表

时间:2011-06-28 03:18:18

标签: c# list binaryfiles

我遇到的问题是我可以让文件准备就绪,但我的读取代码似乎只读取2值列表中的1个值。我对这出错的地方感到难过。代码如下:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;

 namespace test
 {

public partial class Form1 : Form
{
    [Serializable]
    public class ore
    {
        public float Cost;

    }



    private List<ore> oreData = new List<ore>();
    private ore b1 = null;
    private ore b2 = null;
    public Form1()
    {
        InitializeComponent();
        b1 = new ore();
        b2 = new ore();
        oreData.Add(b1);
        oreData.Add(b2);

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;


        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            oreData[0].Cost = tempFloat;
        }
        else
            MessageBox.Show("uh oh");



    }


    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        // 2nd text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            oreData[1].Cost = tempFloat;
        }
        else
            MessageBox.Show("uh oh");


    }


    private void button1_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("ore.dat", FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, oreData);
        fs.Close();
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        // 3rd text box 
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        //4th text box
    }


    private void button2_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("ore.dat", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        oreData = (List<ore>)bf.Deserialize(fs);
        fs.Close();

        if (oreData!=null)
        {
            if (oreData.Count > 0)
                textBox3.Text = oreData[0].Cost.ToString();//update the 3rd text box
            if (oreData.Count > 1)
                textBox4.Text = oreData[1].Cost.ToString();//update the 4th text box

        }
    }
}
}

2 个答案:

答案 0 :(得分:2)

我找到了你的问题! (哦,我的眼睛受伤!)

查看iftextbox1_changed方法中的textbox2_changed语句。他们都是

if (float.TryParse(textBox1.Text, out tempFloat))

但第二个应该说

if (float.TryParse(textBox2.Text, out tempFloat))

请注意textbox1已更改为textbox2。这应该可以解决你的问题。

答案 1 :(得分:0)

您需要定义一个List并向其添加b1,b2项,然后每当您序列化或反序列化数据时,将其反序列化为相同的列表。

注意:将TextBoxes和Buttons重命名为更具可读性的内容也是一个好主意:而不是textBox2 =&gt; FirstBookEpertonTextBoxtextBox2_TextChanged =&gt; firstBookEpertonTextBox_TextChangedOnFirstBookEpertonTextBox_TextChangedbutton1 =&gt; saveBooksButtonForm1 =&gt; BooksMainForm ...

更新另请考虑仅使用列表而不是b1和b2,如:books [0]而不是b1和books [1]而不是b2,所以如果书籍数量增加了代码可维护性不会成为问题。请注意,您正在使用列表来序列化和反序列化。

[Serializable]
public class ore
{
    public float Cost;
}

private List<ore> books = new List<ore>(); // create a list at class level

public Form1()
{
    InitializeComponent();

    books.Add(new ore());
    books.Add(new ore());
}


private void textBox1_TextChanged(object sender, EventArgs e)
{
    // 1st text box input is float
    float tempFloat;
    if (float.TryParse(textBox1.Text, out tempFloat))
    {
        books[0].Cost = tempFloat;
    }
    else
        MessageBox.Show("uh oh");
}


private void textBox2_TextChanged(object sender, EventArgs e)
{
    // 2nd text box input is float
    float tempFloat;
    if (float.TryParse(textBox2.Text, out tempFloat))
    {
        books[1].Cost = tempFloat;
    }
    else
        MessageBox.Show("uh oh");
}

private void button1_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream("ore.dat", FileMode.Create);
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(fs, books);
    fs.Close();
}

private void button2_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream("ore.dat", FileMode.Open);
    BinaryFormatter bf = new BinaryFormatter();

    /*use the old list don't create new one 
    and also the new one you are creating has the same
    name as the class level one which may makes conflicts to you.*/
    books = (List<ore>)bf.Deserialize(fs);

    fs.Close();

    if (books!=null)
    {
        if (books.Count > 0)
        {
            //we don't need d1, d2 any more
            //b1 = books[0];
            textBox3.Text = books[0].Cost.ToString();
        }
        if (books.Count > 1)
        {
            //we don't need d1, d2 any more
            //b2 = books[1];
            textBox4.Text = books[1].Cost.ToString();
        }

    }
}