Published in: C#
/* * FileName: dataList.cs * Classes Included: dataItem, dataList * Purpose: The dataList is a class that contains Name-Value pairs that can be * accessed as per the NameValueCollection, but even though the data is * stored in string format, it can be retrieved in it's native format based * on which accessor method is used. * Created on: 27 May 2009 * Created by: Charles Little * Based on a class created in Delphi by Charles Little. * * History: * Date Initials Note * --------------------------------------------------------------------------- * 05-27-2009 CWL Created * */ using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Collections.Specialized; using System.IO; namespace UtilsLib { /// <summary> /// dataItem stores arbitrary data in an arbitrary format to be retrieved /// similarly to the NameValue collection which it utilizes, to be /// converted or kept in the same format when retrieved. It stores all /// values as string representations, but returns the actual variable /// type of the data depending on which accessor method is utilized. /// </summary> public class dataItem { protected NameValueCollection itemList; protected string itemName; public NameValueCollection ItemList { get { return itemList; } set { itemList = value; } } public string ItemName { get { return itemName; } set { itemName = value; } } public double AsFloat { get { return GetAsFloat(); } set { SetAsFloat(value); } } private void SetAsFloat(double value) { itemList[itemName] = value.ToString(); } private double GetAsFloat() { try { return Convert.ToDouble(itemList[itemName]); } catch { return 0.00; } } public int AsInteger { get { return GetAsInteger(); } set { SetAsInteger(value); } } private void SetAsInteger(int value) { itemList[itemName] = value.ToString(); } private int GetAsInteger() { try { return Convert.ToInt32(itemList[itemName]); } catch { return 0; } } public DateTime AsDateTime { get { return GetAsDateTime(); } set { SetAsDateTime(value); } } private void SetAsDateTime(DateTime value) { itemList[itemName] = value.ToString(); } private DateTime GetAsDateTime() { try { return Convert.ToDateTime(itemList[itemName]); } catch { return Convert.ToDateTime(0); } } public string AsString { get { return GetAsString(); } set { SetAsString(value); } } private void SetAsString(string value) { itemList[itemName] = value; } private string GetAsString() { return itemList[itemName]; } public bool AsBoolean { get { return GetAsBoolean(); } set { SetAsBoolean(value); } } private void SetAsBoolean(bool value) { itemList[itemName] = (value ? "1" : "0"); } private bool GetAsBoolean() { try { return Convert.ToBoolean(itemList[itemName]); } catch { return false; } } public void Initialize() { itemList.Add(itemName, ""); } public void Default(string defaultValue) { if (itemList[itemName].Trim() == "") itemList[itemName] = defaultValue; } public bool Validate() { return (itemList[itemName].Trim() == "") ? false : true; } public void Delete() { itemList.Remove(itemName); } public void Clear() { itemList[itemName] = ""; } public dataItem() { } } /// <summary> /// dataList encapsulates several dataItems to allow the collection to be /// maintained. Items are normally accessed by the Item property, which /// is actually the dataItem in question. /// </summary> public class dataList { protected dataItem internalDataItem; public NameValueCollection InternalDataList { get { return GetInternalDataList(); } set { SetInternalDataList(value); } } private void SetInternalDataList(NameValueCollection value) { internalDataItem.ItemList = value; } private NameValueCollection GetInternalDataList() { return internalDataItem.ItemList; } public virtual dataItem Item(string itemName) { internalDataItem.ItemName = itemName.Trim(); return internalDataItem; } public virtual void Initialize(string itemName) { internalDataItem.ItemName = itemName.Trim(); internalDataItem.Initialize(); } public virtual bool SaveToFile(string fileName) { try { IEnumerator writeDataFile = internalDataItem.ItemList.GetEnumerator(); foreach (string s in internalDataItem.ItemList.AllKeys) DataFile.WriteLine("{0}={1}", s, internalDataItem.ItemList[s]); DataFile.WriteLine(); DataFile.Flush(); DataFile.Close(); return true; } catch (Exception) { return false; } } public virtual bool LoadFromFile(string fileName) { try { if (!File.Exists(fileName)) string[] dataElements; string inLine; while (DataFile.Peek() >= 0) { inLine = DataFile.ReadLine(); dataElements = inLine.Split('='); internalDataItem.ItemList.Add(dataElements[0], dataElements[1]); } return true; } catch (Exception) { return false; } } public virtual void Clear() { InternalDataList.Clear(); } public dataList() { } } }
You need to login to post a comment.
