/// <summary> /// Converts PFKeyValueList object to PFKeyValueListSorted object. /// </summary> /// <param name="kvlist"></param> /// <returns>PFKeyValueListSorted object.</returns> public static PFKeyValueListSorted <K, V> ConvertPFKeyValueListToSortedList(PFKeyValueList <K, V> kvlist) { PFKeyValueListSorted <K, V> kvlistSorted = new PFKeyValueListSorted <K, V>(); kvlist.SetToBOF(); stKeyValuePair <K, V> pfKeyValuePair = kvlist.FirstItem; while (!kvlist.EOF) { kvlistSorted.Add(pfKeyValuePair.Key, pfKeyValuePair.Value); pfKeyValuePair = kvlist.NextItem; } return(kvlistSorted); }
//properties //methods /// <summary> /// Converts PFKeyValueListSorted object to PFKeyValueList object. /// </summary> /// <returns>PFKeyValueList object.</returns> public PFKeyValueList <K, V> ConvertPFKeyValueListSortedToPFKeyValueList(PFKeyValueListSorted <K, V> sortedKvList) { PFKeyValueList <K, V> kvlist = new PFKeyValueList <K, V>(); IEnumerator <KeyValuePair <K, V> > enumerator = sortedKvList.GetEnumerator(); while (enumerator.MoveNext()) { // Get current key value pair stKeyValuePair <K, V> keyValuePair = new stKeyValuePair <K, V>(enumerator.Current.Key, enumerator.Current.Value); kvlist.Add(keyValuePair); } return(kvlist); }
public void GenericSortedKeyValueListTest() { PFKeyValueListSorted <int, string> kvlist = new PFKeyValueListSorted <int, string>(); string kvlistFilename = @"c:\temp\testkvlistsorted.xml"; string connectionString = string.Empty; string configValue = string.Empty; PFDBKeyValueListSortedProcessor <int, string> listProcessor = new PFDBKeyValueListSortedProcessor <int, string>(); try { _msg.Length = 0; _msg.Append("GenericSortedKeyValueListTest started ...\r\n"); Program._messageLog.WriteLine(_msg.ToString()); configValue = AppConfig.GetStringValueFromConfigFile("ListsDatabaseFile", string.Empty); if (configValue.Length > 0) { connectionString = configValue; } else { connectionString = _listsDatabaseConnectionString; } kvlist.Add((int)5, "YFifth value"); kvlist.Add((int)4, "XFourth value"); kvlist.Add((int)3, "Third value"); kvlist.Add((int)2, "Second value"); kvlist.Add((int)1, "First value"); kvlist.SaveToXmlFile(kvlistFilename); PFKeyValueListSorted <int, string> kvlist2 = PFKeyValueListSorted <int, string> .LoadFromXmlFile(kvlistFilename); kvlist2.SetToBOF(); stKeyValuePair <int, string> kvp = kvlist2.FirstItem; while (!kvlist2.EOF) { _msg.Length = 0; _msg.Append(kvp.Key.ToString()); _msg.Append(", "); _msg.Append(kvp.Value); Program._messageLog.WriteLine(_msg.ToString()); kvp = kvlist2.NextItem; } listProcessor.SaveToDatabase(kvlist, connectionString, "TestprogCollectionsObjects_kvlistGenericSorted2"); PFKeyValueListSorted <int, string> kvlist3 = listProcessor.LoadFromDatabase(connectionString, "TestprogCollectionsObjects_kvlistGenericSorted2"); _msg.Length = 0; _msg.Append("kvlist3 ToXmlString Test: "); Program._messageLog.WriteLine(_msg.ToString()); _msg.Length = 0; _msg.Append(kvlist3.ToXmlString()); Program._messageLog.WriteLine(_msg.ToString()); string xmlString3 = kvlist3.ToXmlString(); PFKeyValueListSorted <int, string> kvlist4 = PFKeyValueListSorted <int, string> .LoadFromXmlString(xmlString3); kvlist4.SetToBOF(); kvp = kvlist4.FirstItem; while (!kvlist4.EOF) { _msg.Length = 0; _msg.Append(kvp.Key.ToString()); _msg.Append(", "); _msg.Append(kvp.Value); Program._messageLog.WriteLine(_msg.ToString()); kvp = kvlist4.NextItem; } string xmlFilePath = @"c:\temp\kvlist4_xmldoc.xml"; System.Xml.XmlDocument xmlDoc = kvlist4.ToXmlDocument(); if (File.Exists(xmlFilePath)) { File.Delete(xmlFilePath); } xmlDoc.Save(xmlFilePath); _msg.Length = 0; _msg.Append("XML document created: " + xmlFilePath); Program._messageLog.WriteLine(_msg.ToString()); } catch (System.Exception ex) { _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); Program._messageLog.WriteLine(_msg.ToString()); AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog); } finally { _msg.Length = 0; _msg.Append("\r\n... GenericSortedKeyValueListTest finished."); Program._messageLog.WriteLine(_msg.ToString()); } }
}//end GenericListTest public void GenericKeyValueListTest() { PFKeyValueList <int, string> kvlist = new PFKeyValueList <int, string>(); PFKeyValueList <int, string> kvlist3 = new PFKeyValueList <int, string>(); string kvlistFilename = @"c:\temp\testkvlist.xml"; string connectionString = string.Empty; string configValue = string.Empty; PFDBKeyValueListProcessor <int, string> kvlistProcessor = new PFDBKeyValueListProcessor <int, string>(); try { _msg.Length = 0; _msg.Append("GenericKeyValueListTest started ...\r\n"); Program._messageLog.WriteLine(_msg.ToString()); configValue = AppConfig.GetStringValueFromConfigFile("ListsDatabaseFile", string.Empty); if (configValue.Length > 0) { connectionString = configValue; } else { connectionString = _listsDatabaseConnectionString; } Program._messageLog.WriteLine(Environment.NewLine); kvlist.Add(new stKeyValuePair <int, string>(1, "First value")); kvlist.Add(new stKeyValuePair <int, string>(2, "Second value")); kvlist.Add(new stKeyValuePair <int, string>(3, "Third value")); kvlist.Add(new stKeyValuePair <int, string>(4, "Fourth value is just this")); kvlist.Add(new stKeyValuePair <int, string>(5, "First value is whatever comes here")); kvlist.SaveToXmlFile(kvlistFilename); PFKeyValueList <int, string> kvlist2 = PFKeyValueList <int, string> .LoadFromXmlFile(kvlistFilename); kvlist2.SetToBOF(); stKeyValuePair <int, string> kvp = kvlist2.FirstItem; while (!kvlist2.EOF) { _msg.Length = 0; _msg.Append(kvp.Key.ToString()); _msg.Append(", "); _msg.Append(kvp.Value); Program._messageLog.WriteLine(_msg.ToString()); kvp = kvlist2.NextItem; } kvlistProcessor.SaveToDatabase(kvlist, connectionString, "TestprogCollections_kvlist"); kvlist3 = kvlistProcessor.LoadFromDatabase(connectionString, "TestprogCollections_kvlist"); if (kvlist3 != null) { _msg.Length = 0; _msg.Append("kvlist3 ToXmlString Test: "); Program._messageLog.WriteLine(_msg.ToString()); _msg.Length = 0; _msg.Append(kvlist3.ToXmlString()); _msg.Append(Environment.NewLine); _msg.Append("+++ kvlist3 retrieved. +++"); Program._messageLog.WriteLine(_msg.ToString()); } else { _msg.Length = 0; _msg.Append("kvlist3 ToXmlString Test: "); Program._messageLog.WriteLine(_msg.ToString()); _msg.Length = 0; _msg.Append("*** KVLIST3 IS NULL ***"); Program._messageLog.WriteLine(_msg.ToString()); } } catch (System.Exception ex) { _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); Program._messageLog.WriteLine(_msg.ToString()); AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog); } finally { kvlistProcessor = null; _msg.Length = 0; _msg.Append("\r\n... GenericKeyValueListTest finished."); Program._messageLog.WriteLine(_msg.ToString()); } }