Click here to Skip to main content
15,894,460 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is this good code practice? Pin
PIEBALDconsult1-May-10 5:41
mvePIEBALDconsult1-May-10 5:41 
GeneralRe: Is this good code practice? Pin
venomation1-May-10 7:18
venomation1-May-10 7:18 
GeneralRe: Is this good code practice? Pin
PIEBALDconsult1-May-10 19:41
mvePIEBALDconsult1-May-10 19:41 
GeneralRe: Is this good code practice? [modified] Pin
venomation2-May-10 1:49
venomation2-May-10 1:49 
GeneralRe: Is this good code practice? Pin
PIEBALDconsult2-May-10 4:07
mvePIEBALDconsult2-May-10 4:07 
GeneralRe: Is this good code practice? [modified] Pin
venomation2-May-10 5:58
venomation2-May-10 5:58 
GeneralRe: Is this good code practice? Pin
PIEBALDconsult2-May-10 10:24
mvePIEBALDconsult2-May-10 10:24 
GeneralRe: Is this good code practice? Pin
venomation2-May-10 11:41
venomation2-May-10 11:41 
Thanks again, I have implemented your advise and I can see that it is a much more manageable way that what it originally was, even though there is allot more work involved it does pay off! Laugh | :laugh:

class Program
{

    static void Main()
    {
        Manager<Person> personManager = new Manager<Person>();

        //adding two people for a test purpose
        personManager.Add(new Person("James","x","Soft"));
        personManager.Add(new Person("Claire", "y", "Illu"));

        Menu mainMenu = new Menu(personManager);
        mainMenu.Display();



    }
}

class Manager<T>
    {
        private readonly List<T> _managedList;

        public Manager()
        {
            _managedList = new List<T>(10);
        }

        public void DumpAllToConsole()
        {
            foreach (T p in _managedList)
            {
                Console.WriteLine(p + "\n");
                
            }
        }

        public void Add(T input)
        {
            if (input.Equals(null)) return;

            _managedList.Add(input);

        }

        public void Remove(T item)
        {
           if (!_managedList.Contains(item)) return;
            _managedList.Remove(item);
        }

        public List<T> GetCopyOfList()
        {
            List<T> list = new List<T>(_managedList);
            return list;
        }
     
    }


class Menu
    {
        private readonly Manager<Person> _personManager;
        private delegate void DictionaryDel();
        private Dictionary<string, DictionaryDel> _action;
        private bool _running = true;
        public Menu(Manager<Person> personManager)
        {
            _personManager = personManager;
            LoadActions();

        }

        void DoAction(string input)
        {
            if (_action.Count == 0) return;
            if (!_action.ContainsKey(input)) return;

            _action[input]();
        }



        void LoadActions()
        {
            _action = new Dictionary<string, DictionaryDel>
                          {
                              {"1", _personManager.DumpAllToConsole},
                              {"2",(() => _running = false)}

        };
        //I personally think using the lambda here is ok...anyone care to tell me otherwise? :P

        }

        public void Display()
        {
            while (_running)
            {
                Console.WriteLine("Press 1 to display all students or 2 to quit");
                string input = Console.ReadLine();
                DoAction(input);
                
            }
        }
    }


Thanks again !

Also on a side note, I have just finished reading "The pragmatic programmer, from journeyman to master" and it talks about "orthogonality" which I am positive is the end result from the help I have received !
GeneralRe: Is this good code practice? Pin
PIEBALDconsult2-May-10 17:44
mvePIEBALDconsult2-May-10 17:44 
AnswerRe: Is this good code practice? Pin
venomation3-May-10 0:56
venomation3-May-10 0:56 
GeneralRe: Is this good code practice? Pin
PIEBALDconsult3-May-10 3:59
mvePIEBALDconsult3-May-10 3:59 
GeneralRe: Is this good code practice? Pin
Alaric_3-May-10 4:45
professionalAlaric_3-May-10 4:45 
GeneralRe: Is this good code practice? Pin
venomation3-May-10 5:04
venomation3-May-10 5:04 
QuestionCreate Image in WinForms vs Asp.net Pin
#realJSOP30-Apr-10 9:44
mve#realJSOP30-Apr-10 9:44 
AnswerRe: Create Image in WinForms vs Asp.net Pin
Luc Pattyn30-Apr-10 10:04
sitebuilderLuc Pattyn30-Apr-10 10:04 
GeneralRe: Create Image in WinForms vs Asp.net Pin
#realJSOP30-Apr-10 11:35
mve#realJSOP30-Apr-10 11:35 
GeneralRe: Create Image in WinForms vs Asp.net Pin
Luc Pattyn30-Apr-10 11:41
sitebuilderLuc Pattyn30-Apr-10 11:41 
GeneralRe: Create Image in WinForms vs Asp.net Pin
#realJSOP30-Apr-10 12:04
mve#realJSOP30-Apr-10 12:04 
GeneralRe: Create Image in WinForms vs Asp.net Pin
Luc Pattyn30-Apr-10 12:30
sitebuilderLuc Pattyn30-Apr-10 12:30 
GeneralRe: Create Image in WinForms vs Asp.net Pin
#realJSOP30-Apr-10 23:10
mve#realJSOP30-Apr-10 23:10 
GeneralRe: Create Image in WinForms vs Asp.net Pin
#realJSOP1-May-10 3:07
mve#realJSOP1-May-10 3:07 
GeneralRe: Create Image in WinForms vs Asp.net Pin
Luc Pattyn1-May-10 3:32
sitebuilderLuc Pattyn1-May-10 3:32 
GeneralRe: Create Image in WinForms vs Asp.net Pin
#realJSOP1-May-10 7:35
mve#realJSOP1-May-10 7:35 
GeneralRe: Create Image in WinForms vs Asp.net Pin
Luc Pattyn1-May-10 8:19
sitebuilderLuc Pattyn1-May-10 8:19 
GeneralRe: Create Image in WinForms vs Asp.net Pin
#realJSOP1-May-10 8:47
mve#realJSOP1-May-10 8:47 

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.