We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

wraith808 on 06/29/09


Tagged


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

umang_nine


DataList Class


Published in: C# 


  1. /*
  2.  * FileName: dataList.cs
  3.  * Classes Included: dataItem, dataList
  4.  * Purpose: The dataList is a class that contains Name-Value pairs that can be
  5.  * accessed as per the NameValueCollection, but even though the data is
  6.  * stored in string format, it can be retrieved in it's native format based
  7.  * on which accessor method is used.
  8.  * Created on: 27 May 2009
  9.  * Created by: Charles Little
  10.  * Based on a class created in Delphi by Charles Little.
  11.  *
  12.  * History:
  13.  * Date Initials Note
  14.  * ---------------------------------------------------------------------------
  15.  * 05-27-2009 CWL Created
  16.  *
  17.  */
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Text;
  22. using System.Collections;
  23. using System.Collections.Specialized;
  24. using System.IO;
  25.  
  26. namespace UtilsLib
  27. {
  28. /// <summary>
  29. /// dataItem stores arbitrary data in an arbitrary format to be retrieved
  30. /// similarly to the NameValue collection which it utilizes, to be
  31. /// converted or kept in the same format when retrieved. It stores all
  32. /// values as string representations, but returns the actual variable
  33. /// type of the data depending on which accessor method is utilized.
  34. /// </summary>
  35. public class dataItem
  36. {
  37. protected NameValueCollection itemList;
  38. protected string itemName;
  39.  
  40. public NameValueCollection ItemList { get { return itemList; } set { itemList = value; } }
  41. public string ItemName { get { return itemName; } set { itemName = value; } }
  42.  
  43. public double AsFloat { get { return GetAsFloat(); } set { SetAsFloat(value); } }
  44.  
  45. private void SetAsFloat(double value)
  46. {
  47. itemList[itemName] = value.ToString();
  48. }
  49.  
  50. private double GetAsFloat()
  51. {
  52. try
  53. {
  54. return Convert.ToDouble(itemList[itemName]);
  55. }
  56. catch
  57. {
  58. return 0.00;
  59. }
  60. }
  61.  
  62. public int AsInteger { get { return GetAsInteger(); } set { SetAsInteger(value); } }
  63.  
  64. private void SetAsInteger(int value)
  65. {
  66. itemList[itemName] = value.ToString();
  67. }
  68.  
  69. private int GetAsInteger()
  70. {
  71. try
  72. {
  73. return Convert.ToInt32(itemList[itemName]);
  74. }
  75. catch
  76. {
  77. return 0;
  78. }
  79. }
  80.  
  81. public DateTime AsDateTime { get { return GetAsDateTime(); } set { SetAsDateTime(value); } }
  82.  
  83. private void SetAsDateTime(DateTime value)
  84. {
  85. itemList[itemName] = value.ToString();
  86. }
  87.  
  88. private DateTime GetAsDateTime()
  89. {
  90. try
  91. {
  92. return Convert.ToDateTime(itemList[itemName]);
  93. }
  94. catch
  95. {
  96. return Convert.ToDateTime(0);
  97. }
  98. }
  99.  
  100. public string AsString { get { return GetAsString(); } set { SetAsString(value); } }
  101.  
  102. private void SetAsString(string value)
  103. {
  104. itemList[itemName] = value;
  105. }
  106.  
  107. private string GetAsString()
  108. {
  109. return itemList[itemName];
  110. }
  111.  
  112. public bool AsBoolean { get { return GetAsBoolean(); } set { SetAsBoolean(value); } }
  113.  
  114. private void SetAsBoolean(bool value)
  115. {
  116. itemList[itemName] = (value ? "1" : "0");
  117. }
  118.  
  119. private bool GetAsBoolean()
  120. {
  121. try
  122. {
  123. return Convert.ToBoolean(itemList[itemName]);
  124. }
  125. catch
  126. {
  127. return false;
  128. }
  129. }
  130.  
  131. public void Initialize()
  132. {
  133. itemList.Add(itemName, "");
  134. }
  135.  
  136. public void Default(string defaultValue)
  137. {
  138. if (itemList[itemName].Trim() == "")
  139. itemList[itemName] = defaultValue;
  140. }
  141.  
  142. public bool Validate()
  143. {
  144. return (itemList[itemName].Trim() == "") ? false : true;
  145. }
  146.  
  147. public void Delete()
  148. {
  149. itemList.Remove(itemName);
  150. }
  151.  
  152. public void Clear()
  153. {
  154. itemList[itemName] = "";
  155. }
  156.  
  157. public dataItem()
  158. {
  159. itemList = new NameValueCollection();
  160. }
  161. }
  162.  
  163. /// <summary>
  164. /// dataList encapsulates several dataItems to allow the collection to be
  165. /// maintained. Items are normally accessed by the Item property, which
  166. /// is actually the dataItem in question.
  167. /// </summary>
  168. public class dataList
  169. {
  170. protected dataItem internalDataItem;
  171.  
  172. public NameValueCollection InternalDataList { get { return GetInternalDataList(); } set { SetInternalDataList(value); } }
  173.  
  174. private void SetInternalDataList(NameValueCollection value)
  175. {
  176. internalDataItem.ItemList = value;
  177. }
  178.  
  179. private NameValueCollection GetInternalDataList()
  180. {
  181. return internalDataItem.ItemList;
  182. }
  183.  
  184. public virtual dataItem Item(string itemName)
  185. {
  186. internalDataItem.ItemName = itemName.Trim();
  187.  
  188. return internalDataItem;
  189. }
  190.  
  191. public virtual void Initialize(string itemName)
  192. {
  193. internalDataItem.ItemName = itemName.Trim();
  194. internalDataItem.Initialize();
  195. }
  196.  
  197. public virtual bool SaveToFile(string fileName)
  198. {
  199. try
  200. {
  201. var DataFile = new System.IO.StreamWriter(fileName);
  202. IEnumerator writeDataFile = internalDataItem.ItemList.GetEnumerator();
  203. foreach (string s in internalDataItem.ItemList.AllKeys)
  204. DataFile.WriteLine("{0}={1}", s, internalDataItem.ItemList[s]);
  205. DataFile.WriteLine();
  206. DataFile.Flush();
  207. DataFile.Close();
  208. return true;
  209. }
  210. catch (Exception)
  211. {
  212. return false;
  213. }
  214. }
  215.  
  216. public virtual bool LoadFromFile(string fileName)
  217. {
  218. try
  219. {
  220. if (!File.Exists(fileName))
  221. throw new FileNotFoundException(string.Format("File {0} not found.", fileName));
  222.  
  223. var DataFile = new System.IO.StreamReader(fileName);
  224. string[] dataElements;
  225. string inLine;
  226. while (DataFile.Peek() >= 0)
  227. {
  228. inLine = DataFile.ReadLine();
  229. dataElements = inLine.Split('=');
  230. internalDataItem.ItemList.Add(dataElements[0], dataElements[1]);
  231. }
  232. return true;
  233. }
  234. catch (Exception)
  235. {
  236. return false;
  237. }
  238. }
  239.  
  240. public virtual void Clear()
  241. {
  242. InternalDataList.Clear();
  243. }
  244.  
  245. public dataList()
  246. {
  247. internalDataItem = new dataItem();
  248. }
  249. }
  250. }
  251.  

Report this snippet 

You need to login to post a comment.