Skip to content

Instantly share code, notes, and snippets.

@AdamJHowell
Created October 12, 2016 05:09
Show Gist options
  • Save AdamJHowell/3bfbbb48972825517ebc0dec367b462f to your computer and use it in GitHub Desktop.
Save AdamJHowell/3bfbbb48972825517ebc0dec367b462f to your computer and use it in GitHub Desktop.
Trying to implement INotifyPropertyChanged in my own container.
using System.Collections.Generic;
using System;
using System.ComponentModel;
namespace CS_3260_Lab_07
{
/// <summary>
/// This class will perform the background 'business' logic for our program.
/// </summary>
[Serializable]
public sealed class BusinessRules : INotifyPropertyChanged
{
private SortedDictionary<uint, Employee>sortedEmployees = new SortedDictionary<uint, Employee>();
// Create an event handler to track changes.
public event PropertyChangedEventHandler PropertyChanged;
public BusinessRules()
{
}
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged( Employee emp )
{
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( emp.FirstName ) );
}
/// <summary>
/// This method adds an employee to this container.
/// Currently, this does not check for data validity (such as unique Employee ID numbers).
/// </summary>
/// <param name="toAdd">The Employee class object to add to this BusinessRules container.</param>
public uint AddEmp( Employee toAdd )
{
if ( sortedEmployees.ContainsKey( toAdd.EmpID ) )
{
// Fail
return 0;
}
else
{
// Add the passed object to our SortedDisctionary.
sortedEmployees.Add( toAdd.EmpID, toAdd );
return toAdd.EmpID;
}
}
/// <summary>
/// This method returns an employee class object located at the specified index.
/// </summary>
/// <param name="offset">The key into our SortedDictionary.</param>
/// <returns>An Employee class object, if one exists at that location, otherwise null.</returns>
public Employee GetEmp( uint offset )
{
// Check to make sure the requested location exists, and that it is not empty.
if( sortedEmployees.ContainsKey( offset ) )
{
// Return the requested object.
return sortedEmployees[offset];
}
else
{
// If we reach this code, the requested object does not exist.
return null;
}
}
/// <summary>
/// This method just validates the existance of a key in our container.
/// </summary>
/// <param name="offset">The key to search for.</param>
/// <returns></returns>
public bool Contains( uint offset )
{
// Check to see if the key exists in our SortedDictionary.
if ( sortedEmployees.ContainsKey( offset ) )
{
// Return true to indicate that the value exists as a key in our SortedDictionary.
return true;
}
else
{
// Return false to indicate that the value does not exist as a key in our SortedDictionary.
return false;
}
}
/// <summary>
/// This returns the number of Employee objects in this container.
/// </summary>
/// <returns>An integer representing the quantity of Employee objects in this container.</returns>
public int Size()
{
// Return the count of objects in the SortedDictionary.
return sortedEmployees.Count;
}
/// <summary>
/// This method will remove an employee from the SortedDictionary that has the passed key.
/// </summary>
/// <param name="offset">The key of the employee to remove.</param>
public void DeleteEmp( uint offset )
{
// If the employeeID exists in the SortedDictionary...
if ( sortedEmployees.ContainsKey( offset ) )
{
//...remove the Employee class object from the SortedDictionary.
sortedEmployees.Remove( offset );
}
}
/// <summary>
/// This will return every Employee class object in this container.
/// </summary>
/// <returns>A string containing all data for every Employee in this container.</returns>
public override string ToString()
{
// Create a StringBuilder to hold all Employee information.
System.Text.StringBuilder outString = new System.Text.StringBuilder();
// Parse through every Employee and add it to our StringBuilder.
foreach ( KeyValuePair<uint, Employee>employee in sortedEmployees )
{
// Append the Employee to our StringBuilder.
outString.Append( employee.Value.ToString() );
}
// Return our StringBuilder, which now contains every Employee.
return outString.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment