Guest User

Data Structures with Interface Example File

a guest
Aug 1st, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.68 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace CollectionDemo
  9. {
  10.     public struct Data
  11.     {
  12.         private int _field1;
  13.         private decimal _field2;
  14.         private DateTime _field3;
  15.  
  16.         public int field1
  17.         {
  18.             get
  19.             {
  20.                 return _field1;
  21.             }
  22.             set // This is where I would set format constraints. In VB, this is known as public property....
  23.             {
  24.                 _field1 = value;
  25.             }
  26.         }
  27.  
  28.         public decimal field2
  29.         {
  30.             get
  31.             {
  32.                 return _field2;
  33.             }
  34.             set // This is where I would set format constraints. In VB, this is known as public property....
  35.             {
  36.                 _field2 = value;
  37.             }
  38.         }
  39.         public DateTime field3
  40.         {
  41.             get
  42.             {
  43.                 return _field3;
  44.             }
  45.             set // This is where I would set format constraints. In VB, this is known as public property....
  46.             {
  47.                 _field3 = value;
  48.             }
  49.         }
  50.     }
  51.  
  52.     public class KeyEventArgs : EventArgs
  53.     {
  54.         private string _primaryKey;
  55.         private int _field1;
  56.         private decimal _field2;
  57.         private DateTime _field3;
  58.         public enum activity
  59.         {
  60.             Remove = 0,
  61.             Add = 1,
  62.             ChangedFrom = 2,
  63.             ChangedTo = 3
  64.         }
  65.         private activity _activity;
  66.  
  67.         /// <summary>
  68.         /// Arguments For A Return Of Some Sort...
  69.         /// </summary>
  70.         /// <param name="primaryKey">The Primary Key.</param>
  71.         /// <param name="field1">The Value of the first field.</param>
  72.         /// <param name="field2">The Value of the second field.</param>
  73.         /// <param name="field3">The Value of the third field.</param>
  74.         /// <param name="Activity">Whether the key was created, deleted, or changed.</param>
  75.         public KeyEventArgs(string primaryKey, int field1, decimal field2, DateTime field3, activity Activity){
  76.             this._primaryKey = primaryKey;
  77.             this._field1 = field1;
  78.             this._field2 = field2;
  79.             this._field3 = field3;
  80.             this._activity = Activity;
  81.         }
  82.  
  83.  
  84.         /// <summary>
  85.         /// Arguments For A Return...
  86.         /// </summary>
  87.         /// <param name="primaryKey">The Primary Key.</param>
  88.         /// <param name="statistics">The Data Of The Key.</param>
  89.         /// <param name="Activity">Whether the key was created, deleted, or changed.</param>
  90.         public KeyEventArgs(KeyValuePair<string, Data> reference, activity Activity)
  91.         {
  92.             this._primaryKey = reference.Key;
  93.             this._field1 = reference.Value.field1;
  94.             this._field2 = reference.Value.field2;
  95.             this._field3 = reference.Value.field3;
  96.             this._activity = Activity;
  97.         }
  98.  
  99.         public string PrimaryKey
  100.         {
  101.             get
  102.             {
  103.                 return _primaryKey;
  104.             }
  105.         }
  106.  
  107.         public activity Activity
  108.         {
  109.             get
  110.             {
  111.                 return _activity;
  112.             }
  113.         }
  114.  
  115.         public int Field1
  116.         {
  117.             get
  118.             {
  119.                 return _field1;
  120.             }
  121.         }
  122.  
  123.         public decimal Field2
  124.         {
  125.             get
  126.             {
  127.                 return _field2;
  128.             }
  129.         }
  130.  
  131.         public DateTime Field3
  132.         {
  133.             get
  134.             {
  135.                 return _field3;
  136.             }
  137.         }
  138.     }
  139.     public class KeyChangedEventArgs : EventArgs
  140.     {
  141.         private KeyEventArgs _Current;
  142.         private KeyEventArgs _Obsoleted;
  143.         public KeyChangedEventArgs(KeyValuePair<string, Data> Current, KeyValuePair<string, Data> Obsoleted)
  144.         {
  145.             this._Current = new KeyEventArgs(Current, KeyEventArgs.activity.ChangedTo);
  146.             this._Obsoleted = new KeyEventArgs(Obsoleted, KeyEventArgs.activity.ChangedFrom);
  147.         }
  148.  
  149.         public KeyEventArgs Current
  150.         {
  151.             get
  152.             {
  153.                 return _Current;
  154.             }
  155.         }
  156.  
  157.         public KeyEventArgs Obsoleted
  158.         {
  159.             get
  160.             {
  161.                 return _Obsoleted;
  162.             }
  163.         }
  164.     }
  165.  
  166.  
  167.  
  168.     /// <summary>
  169.     /// A Custom Collection Of Information.
  170.     /// </summary>
  171.     public class CustomCollection : IDictionary<string, Data>
  172.     {
  173.         public delegate void KeyEventHandler(object sender, KeyEventArgs e);
  174.         public delegate void KeyChangedEventHandler(object sender, KeyChangedEventArgs e);
  175.         public event KeyEventHandler onKeyAdded;
  176.         public event KeyEventHandler onKeyRemoved;
  177.         public event KeyChangedEventHandler onKeyChanged;
  178.         private Dictionary<string, Data> Contents;
  179.  
  180.         public CustomCollection()
  181.         {
  182.             Contents = new Dictionary<string, Data>();
  183.         }
  184.  
  185.         /// <summary>
  186.         /// Access To Get Or Set A Specific Index By Primary Key.
  187.         /// </summary>
  188.         /// <param name="key">The Primary Key Of The Data To Access</param>
  189.         /// <returns></returns>
  190.         public Data this[string key]
  191.         {
  192.             get
  193.             {
  194.                 return Contents[key];
  195.             }
  196.  
  197.             set
  198.             {
  199.                 KeyValuePair<string, Data> Old = new KeyValuePair<string, Data>(key, Contents[key]);
  200.                 KeyValuePair<string, Data> New = new KeyValuePair<string, Data>(key, value);
  201.  
  202.                 Contents[key] = value;
  203.  
  204.                 if (onKeyChanged != null) // Send The Event Reporting The Key Has Changed.
  205.                 {
  206.                     onKeyChanged(this, new KeyChangedEventArgs(New, Old));
  207.                 }
  208.             }
  209.         }
  210.  
  211.         /// <summary>
  212.         /// A Count Of The Entries In This Collection.
  213.         /// </summary>
  214.         public int Count
  215.         {
  216.             get
  217.             {
  218.                 return Contents.Count();
  219.             }
  220.         }
  221.  
  222.         /// <summary>
  223.         /// Return whether or not this Collection can be written to.
  224.         /// </summary>
  225.         public bool IsReadOnly
  226.         {
  227.             get
  228.             {
  229.                 return false;
  230.             }
  231.         }
  232.  
  233.         /// <summary>
  234.         /// Returns ALL of the Primary Keys
  235.         /// </summary>
  236.         public ICollection<string> Keys
  237.         {
  238.             get
  239.             {
  240.                 return Contents.Keys;
  241.             }
  242.         }
  243.  
  244.         /// <summary>
  245.         /// Returns ALL of the fields.
  246.         /// </summary>
  247.         public ICollection<Data> Values
  248.         {
  249.             get
  250.             {
  251.                 return Contents.Values;
  252.             }
  253.         }
  254.  
  255.         /// <summary>
  256.         /// Add a entry into this "Table" Per Say...
  257.         /// </summary>
  258.         /// <param name="item">The item to be added.</param>
  259.         public void Add(KeyValuePair<string, Data> item)
  260.         {
  261.             if (Contents.Keys.Contains<string>(item.Key))
  262.             {
  263.                 throw new FormatException("The primary key must be unique.");
  264.             }
  265.  
  266.  
  267.             KeyValuePair<string, Data> Added = item;
  268.             this.Contents.Add(item.Key, item.Value);
  269.  
  270.             if (onKeyAdded != null)
  271.             {
  272.                 this.onKeyAdded(this, new KeyEventArgs(Added, KeyEventArgs.activity.Add));
  273.             }
  274.         }
  275.  
  276.         /// <summary>
  277.         /// Add an entry to this "Table" Per Say...
  278.         /// </summary>
  279.         /// <param name="key">The Primary Key</param>
  280.         /// <param name="value">The Filled Out Data Structure.</param>
  281.         public void Add(string key, Data value)
  282.         {
  283.             if (Contents.Keys.Contains(key))
  284.             {
  285.                 throw new FormatException("The primary key must be unique.");
  286.             }
  287.  
  288.             KeyValuePair<string, Data> Added = new KeyValuePair<string, Data>(key, value);
  289.  
  290.             this.Contents.Add(key, value);
  291.  
  292.             if (onKeyAdded != null)
  293.             {
  294.                 this.onKeyAdded(this, new KeyEventArgs(Added, KeyEventArgs.activity.Add));
  295.             }
  296.         }
  297.  
  298.         /// <summary>
  299.         /// Clears the ENTIRE Collection.
  300.         /// If, unwise (FE: Sensitive Records) one can have this to throw an UnsupportedException Instead.
  301.         /// </summary>
  302.         public void Clear()
  303.         {
  304.             Contents.Clear();
  305.         }
  306.  
  307.         /// <summary>
  308.         /// Checks to see if the item exists.
  309.         /// </summary>
  310.         /// <param name="item">The id and Data structure to check for.</param>
  311.         /// <returns>True if: item exists, Otherwise: False.</returns>
  312.         public bool Contains(KeyValuePair<string, Data> item)
  313.         {
  314.             return Contents.Contains(item);
  315.         }
  316.  
  317.         /// <summary>
  318.         /// Checks to see if the Key Exists.
  319.         /// </summary>
  320.         /// <param name="key">The Primary Keys Value.</param>
  321.         /// <returns>True if: Key Exist, Otherwise: False.</returns>
  322.         public bool ContainsKey(string key)
  323.         {
  324.             return Contents.ContainsKey(key);
  325.         }
  326.  
  327.         public void CopyTo(KeyValuePair<string, Data>[] array, int arrayIndex)
  328.         {
  329.             throw new NotImplementedException();
  330.         }
  331.  
  332.         /// <summary>
  333.         /// If, you call this then obviosly you know what your doing with code.
  334.         /// </summary>
  335.         /// <returns>An Enumerator for the Object.</returns>
  336.         public IEnumerator<KeyValuePair<string, Data>> GetEnumerator()
  337.         {
  338.             return Contents.GetEnumerator();
  339.         }
  340.  
  341.         /// <summary>
  342.         ///
  343.         /// </summary>
  344.         /// <param name="item"></param>
  345.         /// <returns></returns>
  346.         public bool Remove(KeyValuePair<string, Data> item)
  347.         {
  348.             if (Contents.Remove(item.Key) == true)
  349.             {
  350.                 if (onKeyRemoved != null)
  351.                 {
  352.                     this.onKeyRemoved(this, new KeyEventArgs(item, KeyEventArgs.activity.Remove));
  353.                    
  354.                 }
  355.                 return true;
  356.             }
  357.  
  358.             return false;
  359.         }
  360.  
  361.         /// <summary>
  362.         /// Remove The Item, From Our Collection.
  363.         /// </summary>
  364.         /// <param name="key">The Primary Key Of The Item To Remove.</param>
  365.         /// <returns></returns>
  366.         public bool Remove(string key)
  367.         {
  368.             KeyValuePair<string, Data> Removed = new KeyValuePair<string, Data>(key, Contents[key]);
  369.             if (Contents.Remove(key) == true)
  370.             {
  371.                 if (onKeyRemoved != null)
  372.                 {
  373.                     this.onKeyAdded(this, new KeyEventArgs(Removed, KeyEventArgs.activity.Remove));
  374.                 }
  375.                 return true;
  376.             }
  377.             return false;
  378.         }
  379.  
  380.         /// <summary>
  381.         /// Get the key, store its Data in a parameter.
  382.         /// </summary>
  383.         /// <param name="key">The primary key</param>
  384.         /// <param name="value">the Data object to store the key in.</param>
  385.         /// <returns>True if: Key exists, Otherwise: False.</returns>
  386.         public bool TryGetValue(string key, out Data value)
  387.         {
  388.             return Contents.TryGetValue(key, out value);
  389.         }
  390.  
  391.         /// <summary>
  392.         /// Once again, not essential. Not importent.
  393.         /// </summary>
  394.         /// <returns>Absolutely nothing essential to the demo.</returns>
  395.         IEnumerator IEnumerable.GetEnumerator()
  396.         {
  397.             return this.GetEnumerator();
  398.         }
  399.     }
  400. }
Add Comment
Please, Sign In to add comment