M
mp
does this seem a reasonable way to serialize a sorted dictionary?
it works which is cool, but are there issues?
calling code:
sdictCriteriaList_Selected is a SortedDictionary<string,string>
private void btnStoreCriteria_Click(object sender, EventArgs e)
{
CriteriaStore cs = new
CriteriaStore(sdictCriteriaList_Selected);
cs.Store();
}
private void btnRetrieveCriteria_Click(object sender, EventArgs e)
{
SortedDictionary<string, string> sdReadBack;
CriteriaStore cs = new CriteriaStore();
sdReadBack = cs.Retrieve();
//just to see that it's working...
foreach (KeyValuePair<string, string> kvp in sdReadBack)
{ Debug.Print("key " + kvp.Key + " value " + kvp.Value); }
}
the class
[Serializable()]
public class Criteria
{
public string Key { get; set; }
public string Value { get; set; }
public Criteria(string key, string value)
{
Key = key;
Value=value;}
}
class CriteriaStore
{
private SortedDictionary<string, string> _criteriaDictionary;
public CriteriaStore()//to retrieve don't need input arg
{}
public CriteriaStore(SortedDictionary <string,string>
criteriaDictionary)
{
_criteriaDictionary = criteriaDictionary;
}
public void Store()
{
var criteriaList = new List<Criteria>();
foreach(KeyValuePair<string,string> kvp in _criteriaDictionary)
{
criteriaList.Add(new Criteria(kvp.Key,kvp.Value));
}
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, criteriaList);
}
}
catch (IOException)
{
Debug.Print("error storing criteria");
}
}
public SortedDictionary<string, string> Retrieve( )
{
SortedDictionary<string, string> rtnDict = new
SortedDictionary<string, string>();
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
var criteriaList = (List<Criteria>)bin.Deserialize(stream);
foreach (Criteria criteria in criteriaList)
{
rtnDict.Add(criteria.Key, criteria.Value);
}
}
}
catch (IOException)
{
Debug.Print("error retrieving criteria");
}
return rtnDict;
//instead of returning from method, can i fill the byref input
dictionary?
//_criteriaDictionary = rtnDict;
}
}
it works which is cool, but are there issues?
calling code:
sdictCriteriaList_Selected is a SortedDictionary<string,string>
private void btnStoreCriteria_Click(object sender, EventArgs e)
{
CriteriaStore cs = new
CriteriaStore(sdictCriteriaList_Selected);
cs.Store();
}
private void btnRetrieveCriteria_Click(object sender, EventArgs e)
{
SortedDictionary<string, string> sdReadBack;
CriteriaStore cs = new CriteriaStore();
sdReadBack = cs.Retrieve();
//just to see that it's working...
foreach (KeyValuePair<string, string> kvp in sdReadBack)
{ Debug.Print("key " + kvp.Key + " value " + kvp.Value); }
}
the class
[Serializable()]
public class Criteria
{
public string Key { get; set; }
public string Value { get; set; }
public Criteria(string key, string value)
{
Key = key;
Value=value;}
}
class CriteriaStore
{
private SortedDictionary<string, string> _criteriaDictionary;
public CriteriaStore()//to retrieve don't need input arg
{}
public CriteriaStore(SortedDictionary <string,string>
criteriaDictionary)
{
_criteriaDictionary = criteriaDictionary;
}
public void Store()
{
var criteriaList = new List<Criteria>();
foreach(KeyValuePair<string,string> kvp in _criteriaDictionary)
{
criteriaList.Add(new Criteria(kvp.Key,kvp.Value));
}
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, criteriaList);
}
}
catch (IOException)
{
Debug.Print("error storing criteria");
}
}
public SortedDictionary<string, string> Retrieve( )
{
SortedDictionary<string, string> rtnDict = new
SortedDictionary<string, string>();
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
var criteriaList = (List<Criteria>)bin.Deserialize(stream);
foreach (Criteria criteria in criteriaList)
{
rtnDict.Add(criteria.Key, criteria.Value);
}
}
}
catch (IOException)
{
Debug.Print("error retrieving criteria");
}
return rtnDict;
//instead of returning from method, can i fill the byref input
dictionary?
//_criteriaDictionary = rtnDict;
}
}