Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CollectionDemo
- {
- public struct Data
- {
- private int _field1;
- private decimal _field2;
- private DateTime _field3;
- public int field1
- {
- get
- {
- return _field1;
- }
- set // This is where I would set format constraints. In VB, this is known as public property....
- {
- _field1 = value;
- }
- }
- public decimal field2
- {
- get
- {
- return _field2;
- }
- set // This is where I would set format constraints. In VB, this is known as public property....
- {
- _field2 = value;
- }
- }
- public DateTime field3
- {
- get
- {
- return _field3;
- }
- set // This is where I would set format constraints. In VB, this is known as public property....
- {
- _field3 = value;
- }
- }
- }
- public class KeyEventArgs : EventArgs
- {
- private string _primaryKey;
- private int _field1;
- private decimal _field2;
- private DateTime _field3;
- public enum activity
- {
- Remove = 0,
- Add = 1,
- ChangedFrom = 2,
- ChangedTo = 3
- }
- private activity _activity;
- /// <summary>
- /// Arguments For A Return Of Some Sort...
- /// </summary>
- /// <param name="primaryKey">The Primary Key.</param>
- /// <param name="field1">The Value of the first field.</param>
- /// <param name="field2">The Value of the second field.</param>
- /// <param name="field3">The Value of the third field.</param>
- /// <param name="Activity">Whether the key was created, deleted, or changed.</param>
- public KeyEventArgs(string primaryKey, int field1, decimal field2, DateTime field3, activity Activity){
- this._primaryKey = primaryKey;
- this._field1 = field1;
- this._field2 = field2;
- this._field3 = field3;
- this._activity = Activity;
- }
- /// <summary>
- /// Arguments For A Return...
- /// </summary>
- /// <param name="primaryKey">The Primary Key.</param>
- /// <param name="statistics">The Data Of The Key.</param>
- /// <param name="Activity">Whether the key was created, deleted, or changed.</param>
- public KeyEventArgs(KeyValuePair<string, Data> reference, activity Activity)
- {
- this._primaryKey = reference.Key;
- this._field1 = reference.Value.field1;
- this._field2 = reference.Value.field2;
- this._field3 = reference.Value.field3;
- this._activity = Activity;
- }
- public string PrimaryKey
- {
- get
- {
- return _primaryKey;
- }
- }
- public activity Activity
- {
- get
- {
- return _activity;
- }
- }
- public int Field1
- {
- get
- {
- return _field1;
- }
- }
- public decimal Field2
- {
- get
- {
- return _field2;
- }
- }
- public DateTime Field3
- {
- get
- {
- return _field3;
- }
- }
- }
- public class KeyChangedEventArgs : EventArgs
- {
- private KeyEventArgs _Current;
- private KeyEventArgs _Obsoleted;
- public KeyChangedEventArgs(KeyValuePair<string, Data> Current, KeyValuePair<string, Data> Obsoleted)
- {
- this._Current = new KeyEventArgs(Current, KeyEventArgs.activity.ChangedTo);
- this._Obsoleted = new KeyEventArgs(Obsoleted, KeyEventArgs.activity.ChangedFrom);
- }
- public KeyEventArgs Current
- {
- get
- {
- return _Current;
- }
- }
- public KeyEventArgs Obsoleted
- {
- get
- {
- return _Obsoleted;
- }
- }
- }
- /// <summary>
- /// A Custom Collection Of Information.
- /// </summary>
- public class CustomCollection : IDictionary<string, Data>
- {
- public delegate void KeyEventHandler(object sender, KeyEventArgs e);
- public delegate void KeyChangedEventHandler(object sender, KeyChangedEventArgs e);
- public event KeyEventHandler onKeyAdded;
- public event KeyEventHandler onKeyRemoved;
- public event KeyChangedEventHandler onKeyChanged;
- private Dictionary<string, Data> Contents;
- public CustomCollection()
- {
- Contents = new Dictionary<string, Data>();
- }
- /// <summary>
- /// Access To Get Or Set A Specific Index By Primary Key.
- /// </summary>
- /// <param name="key">The Primary Key Of The Data To Access</param>
- /// <returns></returns>
- public Data this[string key]
- {
- get
- {
- return Contents[key];
- }
- set
- {
- KeyValuePair<string, Data> Old = new KeyValuePair<string, Data>(key, Contents[key]);
- KeyValuePair<string, Data> New = new KeyValuePair<string, Data>(key, value);
- Contents[key] = value;
- if (onKeyChanged != null) // Send The Event Reporting The Key Has Changed.
- {
- onKeyChanged(this, new KeyChangedEventArgs(New, Old));
- }
- }
- }
- /// <summary>
- /// A Count Of The Entries In This Collection.
- /// </summary>
- public int Count
- {
- get
- {
- return Contents.Count();
- }
- }
- /// <summary>
- /// Return whether or not this Collection can be written to.
- /// </summary>
- public bool IsReadOnly
- {
- get
- {
- return false;
- }
- }
- /// <summary>
- /// Returns ALL of the Primary Keys
- /// </summary>
- public ICollection<string> Keys
- {
- get
- {
- return Contents.Keys;
- }
- }
- /// <summary>
- /// Returns ALL of the fields.
- /// </summary>
- public ICollection<Data> Values
- {
- get
- {
- return Contents.Values;
- }
- }
- /// <summary>
- /// Add a entry into this "Table" Per Say...
- /// </summary>
- /// <param name="item">The item to be added.</param>
- public void Add(KeyValuePair<string, Data> item)
- {
- if (Contents.Keys.Contains<string>(item.Key))
- {
- throw new FormatException("The primary key must be unique.");
- }
- KeyValuePair<string, Data> Added = item;
- this.Contents.Add(item.Key, item.Value);
- if (onKeyAdded != null)
- {
- this.onKeyAdded(this, new KeyEventArgs(Added, KeyEventArgs.activity.Add));
- }
- }
- /// <summary>
- /// Add an entry to this "Table" Per Say...
- /// </summary>
- /// <param name="key">The Primary Key</param>
- /// <param name="value">The Filled Out Data Structure.</param>
- public void Add(string key, Data value)
- {
- if (Contents.Keys.Contains(key))
- {
- throw new FormatException("The primary key must be unique.");
- }
- KeyValuePair<string, Data> Added = new KeyValuePair<string, Data>(key, value);
- this.Contents.Add(key, value);
- if (onKeyAdded != null)
- {
- this.onKeyAdded(this, new KeyEventArgs(Added, KeyEventArgs.activity.Add));
- }
- }
- /// <summary>
- /// Clears the ENTIRE Collection.
- /// If, unwise (FE: Sensitive Records) one can have this to throw an UnsupportedException Instead.
- /// </summary>
- public void Clear()
- {
- Contents.Clear();
- }
- /// <summary>
- /// Checks to see if the item exists.
- /// </summary>
- /// <param name="item">The id and Data structure to check for.</param>
- /// <returns>True if: item exists, Otherwise: False.</returns>
- public bool Contains(KeyValuePair<string, Data> item)
- {
- return Contents.Contains(item);
- }
- /// <summary>
- /// Checks to see if the Key Exists.
- /// </summary>
- /// <param name="key">The Primary Keys Value.</param>
- /// <returns>True if: Key Exist, Otherwise: False.</returns>
- public bool ContainsKey(string key)
- {
- return Contents.ContainsKey(key);
- }
- public void CopyTo(KeyValuePair<string, Data>[] array, int arrayIndex)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// If, you call this then obviosly you know what your doing with code.
- /// </summary>
- /// <returns>An Enumerator for the Object.</returns>
- public IEnumerator<KeyValuePair<string, Data>> GetEnumerator()
- {
- return Contents.GetEnumerator();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- public bool Remove(KeyValuePair<string, Data> item)
- {
- if (Contents.Remove(item.Key) == true)
- {
- if (onKeyRemoved != null)
- {
- this.onKeyRemoved(this, new KeyEventArgs(item, KeyEventArgs.activity.Remove));
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// Remove The Item, From Our Collection.
- /// </summary>
- /// <param name="key">The Primary Key Of The Item To Remove.</param>
- /// <returns></returns>
- public bool Remove(string key)
- {
- KeyValuePair<string, Data> Removed = new KeyValuePair<string, Data>(key, Contents[key]);
- if (Contents.Remove(key) == true)
- {
- if (onKeyRemoved != null)
- {
- this.onKeyAdded(this, new KeyEventArgs(Removed, KeyEventArgs.activity.Remove));
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// Get the key, store its Data in a parameter.
- /// </summary>
- /// <param name="key">The primary key</param>
- /// <param name="value">the Data object to store the key in.</param>
- /// <returns>True if: Key exists, Otherwise: False.</returns>
- public bool TryGetValue(string key, out Data value)
- {
- return Contents.TryGetValue(key, out value);
- }
- /// <summary>
- /// Once again, not essential. Not importent.
- /// </summary>
- /// <returns>Absolutely nothing essential to the demo.</returns>
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- }
- }
Add Comment
Please, Sign In to add comment