Click here to Skip to main content
14,634,521 members
Home » Discussions » C#
   

C#

 
QuestionGloryfied Calender Control Pin
Harvey Saayman29-Jul-08 21:44
MemberHarvey Saayman29-Jul-08 21:44 
AnswerRe: Gloryfied Calender Control Pin
Simon P Stevens29-Jul-08 23:26
MemberSimon P Stevens29-Jul-08 23:26 
GeneralRe: Gloryfied Calender Control Pin
Harvey Saayman31-Jul-08 22:11
MemberHarvey Saayman31-Jul-08 22:11 
GeneralRe: Gloryfied Calender Control Pin
Simon P Stevens1-Aug-08 2:42
MemberSimon P Stevens1-Aug-08 2:42 
GeneralRe: Gloryfied Calender Control Pin
Harvey Saayman1-Aug-08 2:58
MemberHarvey Saayman1-Aug-08 2:58 
GeneralRe: Gloryfied Calender Control Pin
DaveyM691-Aug-08 3:10
professionalDaveyM691-Aug-08 3:10 
GeneralRe: Gloryfied Calender Control Pin
Harvey Saayman1-Aug-08 3:21
MemberHarvey Saayman1-Aug-08 3:21 
GeneralRe: Gloryfied Calender Control Pin
DaveyM691-Aug-08 3:48
professionalDaveyM691-Aug-08 3:48 
This is something rough I scratched up this morning based on some older code I had used in a similar situation - edited variable and type names to fit for you.

It's a bit long but it should work for the basic data structure!
public struct Employee : IComparable
    {
        public Employee(string name)
        {
            m_Name = name;
        }
        private string m_Name;
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }
        public static bool operator ==(Employee e1, Employee e2)
        {
            return e1.m_Name == e2.m_Name;
        }
        public static bool operator !=(Employee e1, Employee e2)
        {
            return e1.m_Name != e2.m_Name;
        }
        public override bool Equals(object obj)
        {
            if (obj is Employee)
            {
                Employee tmp = (Employee)obj;
                return m_Name.Equals(tmp.m_Name);
            }
            return false;
        }
        public override int GetHashCode()
        {
            return m_Name.GetHashCode();
        }
        public override string ToString()
        {
            return m_Name;
        }
        public int CompareTo(object obj)
        {
            if (obj is Employee)
            {
                Employee tmp = (Employee)obj;
                return m_Name.CompareTo(tmp.m_Name);
            }
            return 1;
        }
    }
    public class EmployeeCollection : List<Employee>
    {
        public event EventHandler ListChanged;
        public new void Add(Employee employee)
        {
            if (!Contains(employee))
            {
                base.Add(employee);
                OnListChanged();
            }
            else
                throw new ArgumentException("Employee exists", "employee");
        }
        protected virtual void OnListChanged()
        {
            if (ListChanged != null)
            {
                ListChanged(this, EventArgs.Empty);
            }
        }
    }
    public struct Time
    {
        public Time(int hour, int minute)
        {
            if (hour > 0 && hour <= 23)
                m_Hour = hour;
            else
                throw new ArgumentOutOfRangeException(
                    "Hour", "Hour must be between 0 and 23");
            if (minute > 0 && minute <= 59)
                m_Minute = minute;
            else
                throw new ArgumentOutOfRangeException(
                    "Minute", "Minute must be between 0 and 59");
        }
        private int m_Hour;
        private int m_Minute;
        public int Hour
        {
            get { return m_Hour; }
            set
            {
                if (value > 0 && value <= 23)
                    m_Hour = value;
                else
                    throw new ArgumentOutOfRangeException(
                        "Hour", "Hour must be between 0 and 23");
            }
        }
        public int Minute
        {
            get { return m_Minute; }
            set
            {
                if (value > 0 && value <= 59)
                    m_Minute = value;
                else
                    throw new ArgumentOutOfRangeException(
                        "Minute", "Minute must be between 0 and 59");
            }
        }
        public override string ToString()
        {
            return m_Hour.ToString().PadLeft(2, '0') +
                m_Minute.ToString().PadLeft(2, '0');
        }
        public DateTime ToDateTime()
        {
            DateTime rtn = new DateTime();
            rtn.AddHours(m_Hour);
            rtn.AddMinutes(m_Minute);
            return rtn;
        }
        public static Time FromDateTime(DateTime value)
        {
            return new Time(value.Hour, value.Minute);
        }
    }
    public struct Shift
    {
        public Shift(Time start, TimeSpan duration)
        {
            m_Start = start;
            m_Duration = duration;
        }
        private Time m_Start;
        private TimeSpan m_Duration;
        public Time Start
        {
            get { return m_Start; }
            set { m_Start = value; }
        }
        public TimeSpan Duration
        {
            get { return m_Duration; }
            set { m_Duration = value; }
        }
        public Time End
        {
            get { return Time.FromDateTime(m_Start.ToDateTime() + m_Duration); }
        }
        public override string ToString()
        {
            return m_Start.ToString() + " - " + End.ToString();
        }
    }
    public class ShiftDictionary : Dictionary<int,Shift>
    {
        public event EventHandler DictionaryChanged;
        private static int lastID = 0;
        public void Add(Shift shift)
        {
            if (!ContainsValue(shift))
            {
                lastID++;
                Add(lastID, shift);
                OnDictionaryChanged();
            }
            else
                throw new ArgumentException("Shift exists", "shift");
        }
        protected virtual void OnDictionaryChanged()
        {
            if (DictionaryChanged != null)
            {
                DictionaryChanged(this, EventArgs.Empty);
            }
        }
    }
    public struct RosterEmployeeItem
    {
        public RosterEmployeeItem(Employee employee, List<int> shifts)
        {
            m_Employee = employee;
            m_Shifts = shifts;
        }
        private Employee m_Employee;
        private List<int> m_Shifts;
        public Employee Employee
        {
            get { return m_Employee; }
            set { m_Employee = value; }
        }
        public List<int> Shifts
        {
            get { return m_Shifts; }
            set { m_Shifts = value; }
        }
        public override string ToString()
        {
            return m_Employee.ToString();
        }
        public string ToString(ShiftDictionary shiftDictionary)
        {
            StringBuilder returnBuilder = new StringBuilder();
            returnBuilder.Append(m_Employee.ToString());
            foreach (int shift in m_Shifts)
            {
                returnBuilder.Append(Environment.NewLine);
                returnBuilder.Append(shiftDictionary[shift].ToString());
            }
            return returnBuilder.ToString();
        }
        public void AddShift(int shift)
        {
            if (!m_Shifts.Contains(shift))
                m_Shifts.Add(shift);
            else
                throw new ArgumentException("Shift exists", "shift");
        }
    }
    public struct RosterShiftItem
    {
        public RosterShiftItem(int shift, List<Employee> employees)
        {
            m_Shift = shift;
            m_Employees = employees;
        }
        private int m_Shift;
        private List<Employee> m_Employees;
        public int Shift
        {
            get { return m_Shift; }
            set { m_Shift = value; }
        }
        public List<Employee> Employees
        {
            get { return m_Employees; }
            set { m_Employees = value; }
        }
        public string ToString(ShiftDictionary shiftDictionary)
        {
            StringBuilder returnBuilder = new StringBuilder();
            returnBuilder.Append(shiftDictionary[m_Shift].ToString());
            foreach (Employee employee in m_Employees)
            {
                returnBuilder.Append(Environment.NewLine);
                returnBuilder.Append(employee.ToString());
            }
            return returnBuilder.ToString();
        }
        public void AddEmployee(Employee employee)
        {
            if (!m_Employees.Contains(employee))
                m_Employees.Add(employee);
            else
                throw new ArgumentException("Employee exists", "employee");
        }
    }

    public struct RosterItem
    {
        public RosterItem(DateTime date)
        {
            m_Date = date.Date;
            m_ItemsByEmployee = new List<RosterEmployeeItem>();
            m_ItemsByShift = new List<RosterShiftItem>();
        }
        private DateTime m_Date;
        private List<RosterEmployeeItem> m_ItemsByEmployee;
        private List<RosterShiftItem> m_ItemsByShift;
        public DateTime Date
        {
            get { return m_Date; }
            set { m_Date = value.Date; }
        }
        public List<RosterEmployeeItem> ItemsByEmployee
        {
            get { return m_ItemsByEmployee; }
            set { m_ItemsByEmployee = value; }
        }
        public List<RosterShiftItem> ItemsByShift
        {
            get { return m_ItemsByShift; }
            set { m_ItemsByShift = value; }
        }
        public string ToString(ShiftDictionary shiftDictionary)
        {
            StringBuilder returnBuilder = new StringBuilder();
            switch (Roster.View)
            {
                case Roster.ViewOptions.ByEmployee:
                    foreach (RosterEmployeeItem item in m_ItemsByEmployee)
                    {
                        returnBuilder.Append(item.Employee.ToString());
                        foreach (int shift in item.Shifts)
                        {
                            returnBuilder.Append(Environment.NewLine);
                            returnBuilder.Append(shiftDictionary[shift].ToString());
                        }
                    }
                    break;
                case Roster.ViewOptions.ByShift:
                    foreach (RosterShiftItem item in m_ItemsByShift)
                    {
                        returnBuilder.Append(shiftDictionary[item.Shift].ToString());
                        foreach (Employee employee in item.Employees)
                        {
                            returnBuilder.Append(Environment.NewLine);
                            returnBuilder.Append(employee.ToString());
                        }
                    }
                    break;
            }
            return returnBuilder.ToString();
        }
        public void AddEmployee(Employee employee, List<int> shifts)
        {
            bool found = false;
            foreach (RosterEmployeeItem employeeItem in ItemsByEmployee)
            {
                if (employeeItem.Employee == employee)
                {
                    foreach (int shift in shifts)
                    {
                        if (!employeeItem.Shifts.Contains(shift))
                        {
                            employeeItem.Shifts.Add(shift);
                            found = true;
                            break;
                        }
                        else
                            throw new ArgumentException("Shift ID " + shift.ToString() + " exists for this employee", "shifts");
                    }
                }
            }
            if (!found)
            {
                m_ItemsByEmployee.Add(new RosterEmployeeItem(employee, shifts));
            }
            foreach (int shift in shifts)
            {
                found = false;
                foreach (RosterShiftItem shiftItem in m_ItemsByShift)
                {
                    if (shiftItem.Shift == shift)
                    {
                        if (!shiftItem.Employees.Contains(employee))
                        {
                            shiftItem.Employees.Add(employee);
                            found = true;
                            break;
                        }
                        else
                            throw new ArgumentException("Employee exists for shift ID " + shift.ToString(), "employee");
                    }
                }
                if (!found)
                {
                    List<Employee> employeeList = new List<Employee>();
                    employeeList.Add(employee);
                    m_ItemsByShift.Add(new RosterShiftItem(shift, employeeList));
                }
            }
        }
    }
    public class Roster : List<RosterItem>
    {
        public static event EventHandler ViewChanged;
        public enum ViewOptions
        {
            ByEmployee, ByShift
        }
        private static ViewOptions m_View;
        public static ViewOptions View
        {
            get { return m_View; }
            set
            {
                ViewOptions oldView = m_View;
                m_View = value;
                if (m_View != oldView)
                    OnViewChanged();
            }
        }
        protected static void OnViewChanged()
        {
            if (ViewChanged != null)
                ViewChanged(null, EventArgs.Empty);
        }
    }


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

GeneralRe: Gloryfied Calender Control Pin
Simon P Stevens1-Aug-08 3:54
MemberSimon P Stevens1-Aug-08 3:54 
GeneralRe: Gloryfied Calender Control Pin
Harvey Saayman1-Aug-08 8:36
MemberHarvey Saayman1-Aug-08 8:36 
AnswerRe: Gloryfied Calender Control Pin
DaveyM6929-Jul-08 23:33
professionalDaveyM6929-Jul-08 23:33 
GeneralRe: Gloryfied Calender Control Pin
Harvey Saayman31-Jul-08 22:13
MemberHarvey Saayman31-Jul-08 22:13 
Questionoverride TreeNode Class in C# Pin
sanjeevmedhi29-Jul-08 21:43
Membersanjeevmedhi29-Jul-08 21:43 
AnswerRe: override TreeNode Class in C# Pin
DaveyM6929-Jul-08 23:37
professionalDaveyM6929-Jul-08 23:37 
GeneralRe: override TreeNode Class in C# Pin
sanjeevmedhi30-Jul-08 23:28
Membersanjeevmedhi30-Jul-08 23:28 
QuestionApplication Crack Pin
Syed Shahid Hussain29-Jul-08 21:38
MemberSyed Shahid Hussain29-Jul-08 21:38 
AnswerRe: Application Crack Pin
leppie29-Jul-08 23:19
Memberleppie29-Jul-08 23:19 
AnswerRe: Application Crack Pin
Ajay.k_Singh29-Jul-08 23:58
MemberAjay.k_Singh29-Jul-08 23:58 
GeneralRe: Application Crack Pin
leppie30-Jul-08 0:29
Memberleppie30-Jul-08 0:29 
AnswerRe: Application Crack Pin
Alan Balkany30-Jul-08 3:43
MemberAlan Balkany30-Jul-08 3:43 
QuestionHi all Pin
tasumisra29-Jul-08 21:35
Membertasumisra29-Jul-08 21:35 
AnswerRe: Hi all Pin
Syed Shahid Hussain29-Jul-08 21:44
MemberSyed Shahid Hussain29-Jul-08 21:44 
GeneralRe: Hi all Pin
tasumisra29-Jul-08 22:44
Membertasumisra29-Jul-08 22:44 
AnswerRe: Hi all Pin
Ravi Kumar Tyagi29-Jul-08 21:46
MemberRavi Kumar Tyagi29-Jul-08 21:46 
GeneralRe: Hi all Pin
tasumisra29-Jul-08 22:46
Membertasumisra29-Jul-08 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.