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
}
}
}
}
button1_Click().List<ore> oreData = new List<ore>()creates the list. 2. The lineList<ore> books = new List<ore>();isn't doing anything, since it is hidden by theList<ore> booksinside thebutton2_Clickmethod.button1_Clickto make sure your listoreDatacontains the data you think it contains prior to serialization.