1

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

        }
    }
}
}
17
  • i am new to C# and im not sure if these lines are creating a new list when i pass a value to them : b1 = new ore(); b2 = new ore(); Commented Jun 28, 2011 at 3:19
  • No. They're just creating the ore objects. If you want a list that contains those objects you need something like what you did in button1_Click(). Commented Jun 28, 2011 at 3:32
  • 1
    Two things - 1. the lines you mentioned in your comment do not create the list. The line List<ore> oreData = new List<ore>() creates the list. 2. The line List<ore> books = new List<ore>(); isn't doing anything, since it is hidden by the List<ore> books inside the button2_Click method. Commented Jun 28, 2011 at 3:32
  • 1
    You might try using a debugger with a breakpoint in button1_Click to make sure your list oreData contains the data you think it contains prior to serialization. Commented Jun 28, 2011 at 3:34
  • okay so number 2 fixed now only: list<ore> books : is there but i still get the value of b1.Titan in both boxes 3 and 4 Commented Jun 28, 2011 at 3:38

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

    }
}

11 Comments

-1: He shouldn't have to deserialize to the same variable. After all, a new list is being created, which can be referred to using any List<ore> variable. In fact, using the same or different variable should have nothing to do with his problem
@Ken: When deserializing from a list, the old list he made at the class level will be obsolete. so he should update it and also update the other values at the class like the b1, b2. Note that he at some time may stop using b1, b2 and only use List due to number of books increase or so.
@Jalal: He doesn't even use the class-level list anywhere in his code - he might as well get rid of it. That list has nothing whatsoever to do with his code. Although your code will work, it does not show the OP what his problem his (in fact, I'm still stumped about it).
He should use it instead of the b1 and b2.. books[0] instead of b1 books[1] instead of b2 and so on...
@Jalal i did change my code to what you suggested if for nothing else it looks cleaner and works better. however the issue still remains i enter 2 values and the same value ( the first one i enter ) displays in both output boxes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.