using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Windows.Documents;
using System.Windows.Input;
namespace WpfApplication1
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
[Serializable]
public class aclass
{
public int level;
public string id;
}
public List
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
aclass ac;
for (int i = 0; i < 5; i++)
{
ac = new aclass();
ac.level = 0;
ac.id = "";
ac.level = i * 4;
ac.id = (i * 12).ToString();
acc.Add(ac);
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Level {0}, ID {1}", acc[i].level, acc[i].id);
}
SaveAsBinaryFormat(acc, "TestSecData.dat");
LoadFromBinaryFile("TestSecData.dat");
//works properly if I put the 3 deserialize logic stmts here (instead of calling "LoadFromBinaryFile"):
//BinaryFormatter binFormat = new BinaryFormatter();
//Stream fStream = File.OpenRead("TestSecData.dat");
//List
}
//------------------------------------------------------------------------------------------------------
static void SaveAsBinaryFormat(object objGraph, string fileName)
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fStream, objGraph);
}
}
//------------------------------------------------------------------------------------------------------
static void LoadFromBinaryFile(string fileName)
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = File.OpenRead(fileName))
{
List
}
// (with count of 5 'aclass' items)
} //but.... 'acci' is cleared here when the method is exited! WHY?
}
}
Would appreciate any help. -Lew
LewisPosted Nov 18, 2010, 5:50 PM
Resolved the problem; involved making 'acci' static, as in:
public static List
Not clear on why it worked, but this will give me time to find out :-)
- Lew