doc
doc

Reputation: 123

reading list from binary file

the problem i am having is that i can get the file to ready but my read code only seems to read 1 value in a 2 value list.i am stumped as to where this is going wrong. code as follows :

 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

        }
    }
}
}

Upvotes: 1

Views: 1581

Answers (2)

Jalal Said
Jalal Said

Reputation: 16162

You need to define a List and add the b1, b2 items to it, then whenever you serialize or deserialize data, deserialize it to the same list.

Note: it is also a good idea to rename your TextBoxes and Buttons to something more readable like: instead of textBox2=> FirstBookEpertonTextBox, textBox2_TextChanged=> firstBookEpertonTextBox_TextChanged or OnFirstBookEpertonTextBox_TextChanged, button1=> saveBooksButton, Form1=> BooksMainForm....

Update: Also consider use the list only instead of b1 and b2 like: books[0] instead of b1 and books[1] instead of b2 and so if the number of books increases the code maintainability will not be a problem. note that you are using a list to serialize and deserialize anyway.

[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();
        }

    }
}

Upvotes: 0

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19347

I found your problem! (Oh my eyes hurt!)

Look at the if statement in your textbox1_changed and textbox2_changed methods. They are both

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

But the second one should say

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

Notice that textbox1 is changed to textbox2. This should solve your problem.

Upvotes: 2

Related Questions