Exemplo n.º 1
0
        static void CastingExamples()
        {
            //A Manager "is-a" System.Object, so we can
            //store a Manager reference in an object variable just fine
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            GivePromotion((Manager)frank);
            Hexagon hex;

            try
            {
                hex = (Hexagon)frank;
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine(ex.Message);
            }

            //A Manager "is-an" Employee too
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit); //send as argument - Manager Instead Employee

            //A PTSalesPerson "is-a" SalesPerson
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);

            GivePromotion(jill); //send as argument - PTSalesPerson instead Employee
        }
Exemplo n.º 2
0
        static void Casting()
        {
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.WriteLine("=> Casting Rules");

            object      frank = new Manager("Frank", 9, 3000, 40000, "111111111", 5);
            Employee    moon  = new Manager("Moon", 2, 3001, 20000, "101111111", 10);
            SalesPerson jill  = new PTSalesPerson("Jill", 834, 3002, 100002, "102111111", 90);

            GivePromotion(moon); GivePromotion(jill);
            GivePromotion((Manager)frank);

            object obj = new object();

            try
            {
                GivePromotion((Manager)obj);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Erorr for GiveGromotion(obj): {e.Message}");
            }

            Console.WriteLine($"Types: frank <{frank.GetType().Name}>, moon <{moon.GetType()}>, jill <{jill.GetType().Name}>, obj <{obj.GetType()}>");
        }
Exemplo n.º 3
0
        static void CastingExamples()
        {
            // A Manager "is-a" System.Object, so we can
            // store a Manager reference in an object variable just fine.
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            GivePromotion((Manager)frank);

            // A Manager "is-an" Employee too.
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit);

            // A PTSalesPerson "is-a" SalesPerson.
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);

            GivePromotion(jill);
            // Hexagon hex = (Hexagon)frank;
            // Use "as" to test compability.
            Hexagon hex2 = frank as Hexagon;

            if (hex2 == null)
            {
                Console.WriteLine("Sorry, frank is not a Hexagon...");
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy *****\n");

            SalesPerson fred = new SalesPerson();

            fred.Age         = 32;
            fred.Name        = "Fred";
            fred.SalesNumber = 50;

            Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2332", 9000);

            chucky.GiveBonus(300);
            chucky.DisplayStats();
            Console.WriteLine();

            SalesPerson frank = new SalesPerson("Frank", 43, 93, 3000, "932-23-3232", 31);

            frank.GiveBonus(200);
            frank.DisplayStats();

            CastingExamples();

            //Привести объект Manager к объекту Hexagon невозможно
            //но этот код нормально скомпилируется!
            try
            {
                object  ted = new Manager();
                Hexagon hex = (Hexagon)ted;
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine($"{ex.Message}\n");
            }

            //Использование ключевого слова as для проверки совместимости приводимых типов
            object[] things = new object[5];
            things[0] = new SalesPerson();
            things[1] = false;
            things[2] = new Manager();
            things[3] = "Last thing";
            things[4] = new PTSalesPerson();

            foreach (object item in things)
            {
                Employee h = item as SalesPerson;
                if (h == null)
                {
                    Console.WriteLine($"Item is not SalesPerson");
                }
                else
                {
                    Console.WriteLine($"{item} is SalesPerson");
                }
            }

            Console.ReadLine();
        }
Exemplo n.º 5
0
        static void CastingExamples()
        {
            // A Manager "is-a" System.Object, so we can
            // store a Manager reference in an object variable just fine.
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            // Error!
            //GivePromotion(frank);
            // OK!
            GivePromotion((Manager)frank);

            // Ack! You can't cast frank to a Hexagon, but this compiles fine!
            //Hexagon hex = (Hexagon)frank;

            // Catach a possible invalid cast.
            Hexagon hex;

            try
            {
                hex = (Hexagon)frank;
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine(ex.Message);
            }

            // Use "as" to test compatability.
            object[] things = new object[4];
            things[0] = new Hexagon();
            things[1] = false;
            things[2] = new Manager();
            things[3] = "Last thing";

            foreach (object item in things)
            {
                Hexagon h = item as Hexagon;
                if (h == null)
                {
                    Console.WriteLine("Item is not a hexagon");
                }
                else
                {
                    h.Draw();
                }
            }

            // A Manager "is-a" Employee too.
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit);

            // A PTSalesPerson "is-a" SalesPerson.
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);

            GivePromotion(jill);
        }
Exemplo n.º 6
0
        static void CastingExamples()
        {
            object      frank    = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);
            Employee    moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);
            SalesPerson jill     = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1118", 90);

            GivePromotion(moonUnit);
            GivePromotion(jill);
            GivePromotion((Manager)frank);
        }
Exemplo n.º 7
0
        // Populate an initial set of Employees
        void InitialEmployees()
        {
            // Create Employees
            Executive     dan    = new Executive("Dan", "Brown", DateTime.Parse("3/20/1963"), 200000, "121-12-1211", 50000, ExecTitle.CEO);
            Executive     connie = new Executive("Connie", "Chung", DateTime.Parse("2/5/1972"), 150000, "229-67-7898", 40000, ExecTitle.CFO);
            Manager       chucky = new Manager("Chucky", "Jones", DateTime.Parse("4/23/1967"), 100000, "333-23-2322", 9000);
            Manager       mary   = new Manager("Mary", "Williams", DateTime.Parse("5/9/1963"), 200000, "121-12-1211", 9500);
            Engineer      bob    = new Engineer("Bob", "Roe", DateTime.Parse("6/30/1986"), 120000, "334-24-2422", DegreeName.MS);
            SalesPerson   fran   = new SalesPerson("Fran", "Smith", DateTime.Parse("7/5/1975"), 80000, "932-32-3232", 31);
            PTSalesPerson sam    = new PTSalesPerson("Sam", "Abbott", DateTime.Parse("8/11/1984"), 20000, "525-76-5030", 20);
            PTSalesPerson sally  = new PTSalesPerson("Sally", "McRae", DateTime.Parse("9/12/1979"), 30000, "913-43-4343", 10);
            SupportPerson mike   = new SupportPerson("Mike", "Roberts", DateTime.Parse("10/31/1975"), 15000, "229-67-7898", ShiftName.One);
            SupportPerson steve  = new SupportPerson("Steve", "Kinny", DateTime.Parse("11/21/1982"), 80000, "913-43-4343", ShiftName.Two);

            // Bonuses and promotions
            dan.GiveBonus(1000);
            bob.GiveBonus(500);
            sally.GiveBonus(400);
            dan.GivePromotion();
            chucky.GivePromotion();
            fran.GivePromotion();

            // Add reports - just report error and continue on exception
            try
            {
                dan.AddReport(chucky);
                dan.AddReport(mary);
                connie.AddReport(fran);
                connie.AddReport(sally);
                mary.AddReport(sam);
                mary.AddReport(mike);
                chucky.AddReport(bob);
                chucky.AddReport(steve);
            }
            catch (Exception e)
            {
                MessageBox.Show($"Error adding reports: {e.Message}");
            }

            // Add Expenses
            dan.Expenses.Add(new Expense(DateTime.Parse("1/25/2017"), ExpenseCategory.Travel, "Airline Tickets - San Diego", 300.55));
            dan.Expenses.Add(new Expense(DateTime.Parse("1/27/2017"), ExpenseCategory.Meals, "Breakfast at Hotel", 27.61));
            dan.Expenses.Add(new Expense(DateTime.Parse("1/29/2017"), ExpenseCategory.Lodging, "Conference Hotel - 5 nights", 423.15));

            // Add Employees to list
            List <Employee> emps = new List <Employee>()
            {
                dan, connie, chucky, mary, bob, fran, sam, sally, mike, steve
            };

            foreach (Employee e in emps)
            {
                Add(e);
            }
        }
Exemplo n.º 8
0
        public void CastingExamples()
        {
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);
            GivePromotion((Manager)frank);

            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);
            GivePromotion(moonUnit);

            SalesPerson jill = new PTSalesPerson("Jill", 834, 3001, 100000, "111-12-1119", 90);
            GivePromotion(jill);
        }
Exemplo n.º 9
0
        static void CastingExamples()
        {
            // Manager "is-a" System.Object, so we can
            // store a manager reference in an onject variable
            object frank = new Manager("Frank", 9, 300, 40000, "111-11-111", 5);

            // A Manager "is-an" Employee too
            Employee moonUnit = new Manager("MoonUnit", 2, 3001, 2000, "111-11-1234", 1);

            // A PTSalesPerson "is-a" SalesPerson
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 10000, "1231", 90);
        }
Exemplo n.º 10
0
        static void CastingExamples()
        {
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "222-22-2222", 5);

            //Manager "is-an" employee too
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit);

            //PTSales "is-an" SalesPerson too
            SalesPerson Jill = new PTSalesPerson("Jill", 834, 3002, 10000, "111-12-1119", 90);
        }
Exemplo n.º 11
0
        static void CastingExamples()
        {
            // A Manager "is-a" System.Object, so we can
            // store a Manager reference in an object variable just fine.
            object      frank    = new Manager("Frank Zappa", 9, 3012, 40000, "1978-06-27-4572", 5);
            Employee    moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "1897-04-13-5167", 1);
            SalesPerson jill     = new PTSalesPerson("Jill", 834, 3002, 100000, "2017-06-2673", 90);

            GivePromotion(moonUnit);
            GivePromotion(jill);
            // To promote frank we need to use an explicit class.
            GivePromotion((Manager)frank);
        }
Exemplo n.º 12
0
        static void CastingExamples()
        {
            // A Manager "is-a" System.Object, so we can
            // store a Manager reference in an object variable just fine.
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);
            GivePromotion((Manager)frank);

            // A Manager "is-an" Employee too.
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);
            GivePromotion(moonUnit);

            // A PTSalesPerson "is-a" SalesPerson.
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);
            GivePromotion(jill);
        }
Exemplo n.º 13
0
        static void CastingExamples()
        {
            // A Manager "is-a" System.Object, so we can
            // store a Manager reference in an object variable just fine.
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            // A Manager "is-an" Employee too.
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit);

            // A PTSalesPerson "is-a" SalesPerson.
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);

            GivePromotion(jill);
        }
Exemplo n.º 14
0
        static void FunWithCasting()
        {
            // A Manager "is-a" System.Object.
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            // A Manager "is-a" Employee too.
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            // A PTSalesPerson "is-a" SalesPerson.
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);

            // Streamline the staff.
            FireThisPerson(moonUnit);
            FireThisPerson(jill);
            FireThisPerson((Manager)frank);
        }
Exemplo n.º 15
0
        public static int Main(string[] args)
        {
            Console.WriteLine("These folks work at " + Employee.Company);
            Console.WriteLine();

            // Uncomment to test abstract base class.
            // Employee e = new Employee();

            Manager chucky = new Manager("Chucky", 92, 100000, "333-23-2322", 9000);

            chucky.GiveBonus(300);
            chucky.DisplayStats();

            Console.WriteLine();

            SalesPerson fran = new SalesPerson("Fran", 93, 30000, "932-32-3232", 31);

            fran.GiveBonus(201);
            fran.DisplayStats();

            // Casting!

            // A Manager 'is-a' object.
            object o = new Manager("Frank Zappa", 9, 40000, "111-11-1111", 5);

            // A Manager 'is-a' Employee too.
            Employee e = new Manager("MoonUnit Zappa", 2, 20000, "101-11-1321", 1);

            // A PTSales dude(tte) is a Sales dude(tte)
            SalesPerson sp = new PTSalesPerson("Jill", 834, 100000, "111-12-1119", 90);

            FireThisPerson(e);
            FireThisPerson(sp);

            // Error!  Must cast when moving from base to derived!
            // FireThisPerson(o);
            FireThisPerson((Employee)o);

            // Numerical casts...
            int  x = 30000;
            byte b = (byte)x;   // Loss of information here…



            return(0);
        }
Exemplo n.º 16
0
        static void CastingExamples()
        {
            object   frank = new Manager("Frank", 93, 12, 34.3f, 18);
            Employee kurt  = new Manager("Kurt", 23, 1, 13, 8);

            GivePromotion(kurt);
            SalesPerson nick = new PTSalesPerson("Nick", 45, 15, 18, 29);

            GivePromotion(nick);
            GivePromotion((Manager)frank);
            PTSalesPerson pt = frank as PTSalesPerson;

            if (pt == null)
            {
                Console.WriteLine("Frank is not a sales manager");
            }
        }
Exemplo n.º 17
0
        static List <Employee> InitialEmployees()
        {
            Executive     dan    = new Executive("Dan", "Doe", DateTime.Parse("3/20/1963"), 200000, "121-12-1211", 50000, ExecTitle.CEO);
            Executive     connie = new Executive("Connie", "Chung", DateTime.Parse("2/5/1972"), 150000, "229-67-7898", 40000, ExecTitle.CFO);
            Manager       chucky = new Manager("Chucky", "Kirkland", DateTime.Parse("4/23/1967"), 100000, "333-23-2322", 9000);
            Manager       mary   = new Manager("Mary", "Tompson", DateTime.Parse("5/9/1963"), 200000, "121-12-1211", 9500);
            Engineer      bob    = new Engineer("Bob", "Pirs", DateTime.Parse("6/30/1986"), 120000, "334-24-2422", DegreeName.MS);
            SalesPerson   fran   = new SalesPerson("Fran", "Smith", DateTime.Parse("7/5/1975"), 80000, "932-32-3232", 31);
            PTSalesPerson sam    = new PTSalesPerson("Sam", "Abbot", DateTime.Parse("8/11/1984"), 20000, "525-76-5030", 20);
            PTSalesPerson sally  = new PTSalesPerson("Sally", "McRae", DateTime.Parse("9/12/1979"), 30000, "913-43-4343", 10);
            SupportPerson mike   = new SupportPerson("Mike", "Roberts", DateTime.Parse("10/31/1975"), 15000, "229-67-7898", ShiftName.One);
            SupportPerson steve  = new SupportPerson("Steve", "Kinny", DateTime.Parse("11/21/1982"), 80000, "913-43-4343", ShiftName.Two);


            // Bonuses and promotions
            dan.GiveBonus(1000);
            bob.GiveBonus(500);
            sally.GiveBonus(400);
            dan.GivePromotion();
            chucky.GivePromotion();
            fran.GivePromotion();


            // Add reports - just report error and continue on exception
            try
            {
                dan.AddReport(chucky);
                dan.AddReport(mary);
                connie.AddReport(fran);
                connie.AddReport(sally);
                mary.AddReport(sam);
                mary.AddReport(mike);
                chucky.AddReport(bob);
                chucky.AddReport(steve);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error when adding reports: {e.Message}");
            }

            return(new List <Employee>()
            {
                dan, connie, chucky, mary, bob, fran, sam, sally, mike, steve
            });
            //return new List<Employee>() { dan, connie, chucky };
        }
Exemplo n.º 18
0
        static void CastingExamples()
        {
            //Manager是(is-a) System.Object,因此我们刚好可以在object变量中存储Manage引用
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            GivePromotion((Manager)frank);

            //Manager同样是Employee
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit);

            //  PTSalesPerson是SalesPerson
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);

            GivePromotion(jill);
        }
Exemplo n.º 19
0
        static void CastingExamples()
        {
            //Manager является System.Object, поэтому в переменной
            //типа Object можно хранить ссылку на Manager
            object fran = new Manager("Fran Zappa", 9, 3000, 40000, "111-11-1111", 5);

            //GivePromotion(fran) - Ошибка! Object не является Manager
            GivePromotion((Manager)fran); //Верно! Явное приведение

            //Manager также является Employee
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit); //Пример неявного приведения

            //PTSalesPerson является SalesPerson
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1191", 90);

            GivePromotion(jill); //Примеры неявного приведения
        }
Exemplo n.º 20
0
        static void CastingExamples()
        {
            object   frank    = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit);
            Salesperson   jill     = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90, 4);
            PTSalesPerson TestPTSP = new PTSalesPerson("TestPTSP", 22, 99, 20000, "13337", 20, 4);

            GivePromotion((Manager)frank);
            GivePromotion((Salesperson)TestPTSP);

            Salesperson sp = frank as Salesperson;

            if (sp == null)
            {
                Console.WriteLine("Sorry, frank is not a salesperson...");
            }
        }
Exemplo n.º 21
0
        static void CastingExamples()
        {
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            GivePromotion((Manager)frank);
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit);
            //PtSalesPerson "является" SalesPerson
            Salesperson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111 - 12 - 1119", 90);

            GivePromotion(jill);
            // Использование as для проверки соместимости.
            Salesperson hex2 = frank as Salesperson;

            if (hex2 == null)
            {
                Console.WriteLine("Sorry, frank is not a Salesperson");
            }
        }
Exemplo n.º 22
0
        static void Subclasses()
        {
            Console.ForegroundColor = ConsoleColor.Green;

            SalesPerson fred = new SalesPerson {
                Age = 31, Name = "Fred", SalesNumber = 50
            };

            Console.WriteLine($"SalesPerson: {fred.Name}, {fred.SalesNumber}, {fred.Age}");

            PTSalesPerson jason = new PTSalesPerson {
                Age = 26, Name = "Jason", SalesNumber = 35
            };

            Console.WriteLine($"Part-time SalesPerson: {jason.Name}, {jason.SalesNumber}, {jason.Age}");

            Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000);

            Console.WriteLine($"Manager: {chucky.Name}, {chucky.StockOptions}, {chucky.Age}");
        }
Exemplo n.º 23
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy ******\n");
            Salesperson fred = new Salesperson();

            fred.Age         = 31;
            fred.Name        = "Fred";
            fred.SalesNumber = 50;

            Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000);
            double  cost   = chucky.GetBenefitCost();

            Console.WriteLine("Chucky benefit cost: {0}", cost);

            PTSalesPerson TestPTSP = new PTSalesPerson("Alex", 24, 93, 500000, "1337-1337", 4000, 4);

            Console.WriteLine("SalesPerson {0} works {1} hours / day", TestPTSP.Name, TestPTSP.WorkHours);


            Console.ReadLine();
        }
Exemplo n.º 24
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy ****\n");

            Manager chucky = new Manager("Cucky", 50, 92, 100000, "333-23-2322", 9000);

            chucky.GiveBonus(300);
            chucky.DisplayStats();
            Console.WriteLine();

            SalesPerson fran = new SalesPerson("Fran", 43, 93, 300, "932-32-3232", 31);

            fran.GiveBonus(200);
            fran.DisplayStats();

            // A Manager "is-a" System.Object, so we can
            // store a Manager reference in an object variable just fine.
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "123", 5);

            GivePromotion((Manager)frank); // explicit casting, yay!

            //A Manager "is-an" Employee too
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "567", 1);

            GivePromotion(moonUnit);

            // A PTSalesPerson "is-a" SalesPerson
            SalesPerson jill = new PTSalesPerson("Jill", 834, 30001, 123123, "123124", 90);

            GivePromotion(jill);

            // Use "as" to test compatability

            /* Hexagon hex2 = frank as Hexagon;
             * if (hex2 == null)
             *      Console.WriteLine("Sorry, frank is not a Hexagon...");
             */

            Console.ReadLine();
        }
Exemplo n.º 25
0
        static void CastingExamples() {
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            //GivePromotion(frank);//doesn't work bc "frank" is stored in a System.Object, which is higher up 
            //than Employee on the inheritance chain, so an implicit cast can't happen...
            //instead we must EXPLICITLY cast it along the lines of (ClassIWantToCastTo)referenceIHave...

            //even though it makes NO sense to cast frank to a Hexagon, you can! 
            //***Hexagon hex = (Hexagon)frank;*** and it compiles!
            //Nothing would blow up until runtime when you'd get an exception
            //When you are explicitly casting you can use a try/catch to check for compatibility...
            //Hexagon hex;
            //try
            //{
            //    hex = (Hexagon)frank;
            //}
            //catch (InvalidCastException ex)
            //{

            //    Console.WriteLine(ex.Message);            }

            //you can also test for compatibility via the "as" keyword...
            //object[] things = new object[4];
            //things[0] = new Hexagon();
            //things[1] = false;
            //things[2] = new Manager();
            //things[3] = "Last Thing";

            //foreach (object item in things)
            //{
            //    Hexagon h = item as Hexagon;
            //    if (h == null)
            //    {
            //        Console.WriteLine("Item is not a hexagon");
            //    }
            //    else
            //        h.Draw();
            //}

            //comparisons can also be made with the "is" keyword.  This checks for a return of flase, rather 
            //than a null value as was seen with "as" keyword.  "is" doesn't doesn't perform a cast, it just 
            //checks compatibility so that you can subsequenly cast as you like(w/o a PITA try/catch)

            GivePromotion((Manager)frank);
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1111", 1);
            GivePromotion(moonUnit);
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 10000, "111-22-3333", 90);
            GivePromotion(jill);
            
        }
        static void CastingExamples()
        {
            //  A Manager "is-a" System.Object, so we can store a
            //  Manager reference in an object variable just fine.
            object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);

            //  Error! Cannot pass 'System.Object' to 'Employee'
            //GivePromotion(frank);
            //  OK!
            GivePromotion((Manager)frank);

            //  A Manager "is-an" Employee too.
            Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);

            GivePromotion(moonUnit);

            //  A PTSalesPerson "is-a" SalesPerson.
            SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);

            GivePromotion(jill);

            //  Ack! You can't cast frank to a Hexagon, but this
            //  compiles fine!
            //  Would recieve runtime exception.
            frank = new Manager();
            //Hexagon hex = (Hexagon)frank;
            //  Catch a possible invalid cast.
            Hexagon hex;

            try
            {
                hex = (Hexagon)frank;
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine(ex.Message);
            }

            //  Use "as" to test compatibilty.
            //  "as" returns a null reference.
            object[] things = new object[4];
            things[0] = new Hexagon();
            things[1] = false;
            things[2] = new Manager();
            things[3] = "Last thing";

            foreach (object item in things)
            {
                Hexagon h = item as Hexagon;
                if (h == null)
                {
                    Console.WriteLine("Item is not a hexagon");
                }
                else
                {
                    h.Draw();
                }
            }

            //  Discards with the "is" keyword
            //  use as catchall in if or switch statement
            if (frank is var _)
            {
                //  do something
            }
        }
Exemplo n.º 27
0
        static void Main(string[] args)
        {
            #region Uncomment to test abstract base class.
            // Employee e = new Employee();

            // Uncomment to test readonly field.
            // Employee brenner = new Employee();
            // brenner.SSNField = "666-66-6666";
            #endregion

            Console.WriteLine("***** These folks work at {0} *****",
                              Employee.Company);

            Manager chucky = new Manager("Chucky", 35, 92, 100000, "333-23-2322", 9000);
            chucky.GiveBonus(300);
            chucky.DisplayStats();
            chucky.Age++;   // Happy birthday Chuck!
            Console.WriteLine();

            SalesPerson fran = new SalesPerson("Fran", 26, 93, 30000, "932-32-3232", 31);
            fran.GiveBonus(200);
            fran.DisplayStats();
            Console.WriteLine();

            #region Casting examples
            Console.WriteLine("***** Casting examples *****");
            // A Manager 'is-a' object.
            object frank = new Manager("Frank Zappa", 50, 9, 40000, "111-11-1111", 5);

            // A Manager 'is-a' Employee too.
            Employee moonUnit = new Manager("MoonUnit Zappa", 16, 2, 20000, "101-11-1321", 1);

            // A PTSales dude(tte) is a Sales dude(tte)
            SalesPerson jill = new PTSalesPerson("Jill", 30, 834, 100000, "111-12-1119", 90);

            // Cast from the generic System.Object into a strongly
            // typed Manager.
            Console.WriteLine("***** Counting frank's stock options *****");
            Manager mgr = (Manager)frank;
            Console.WriteLine("Frank's options: {0}", mgr.NumbOpts);
            Console.WriteLine("Frank's options: {0}",
                              ((Manager)frank).NumbOpts);

            Console.WriteLine("\n***** Firing employees using casting operations! *****");
            TheMachine.FireThisPerson(moonUnit);
            Console.WriteLine();
            TheMachine.FireThisPerson(jill);
            Console.WriteLine();

            // Error!  Must cast when moving from base to derived!
            // TheMachine.FireThisPerson(frank);

            TheMachine.FireThisPerson((Employee)frank);
            Console.WriteLine();

            #endregion

            // Creating a (very) nested BenefitPackageLevel enum variable.
            Employee.BenefitPackage.BenefitPackageLevel myBenifitLevel =
                Employee.BenefitPackage.BenefitPackageLevel.Platinum;

            Console.ReadLine();
        }
Exemplo n.º 28
0
        static void Main()
        {
            Console.WriteLine("***** Assignment 4: Employee Reports *****\n");

            // Create Employees
            Manager       chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000);
            Manager       mary   = new Manager("Mary", 54, 90, 200000, "121-12-1211", 9500);
            SalesPerson   fran   = new SalesPerson("Fran", 43, 93, 80000, "932-32-3232", 31);
            SalesPerson   bob    = new SalesPerson("Bob", 31, 94, 120000, "334-24-2422", 30);
            PTSalesPerson sally  = new PTSalesPerson("Sally", 32, 95, 30000, "913-43-4343", 10);
            PTSalesPerson sam    = new PTSalesPerson("Sam", 33, 96, 20000, "525-76-5030", 20);
            PTSalesPerson mike   = new PTSalesPerson("Mike", 45, 91, 15000, "229-67-7898", 30);

            // Employee list
            Employee[] emps = { chucky, mary, fran, bob, sally, sam, mike };

            // Add reports
            try
            {
                mary.AddReport(fran);
                mary.AddReport(mike);
                chucky.AddReport(bob);
                chucky.AddReport(sally);
                chucky.AddReport(sam);
                mary.RemoveReport(fran);
                mary.RemoveReport(chucky);
                chucky.AddReport(fran);
                chucky.AddReport(mike);
                chucky.AddReport(mary);
            }
            catch (Manager.TooManyReportsException e)
            {
                Console.WriteLine("*** Error! ***");
                Console.WriteLine("Source: {0}", e.Source);
                Console.WriteLine("Method: {0}", e.TargetSite);
                Console.WriteLine("Message: {0}", e.Message);
                Console.WriteLine("Custom Data:");
                foreach (DictionaryEntry de in e.Data)
                {
                    Console.WriteLine("-> {0}: {1}", de.Key, de.Value);
                }
            }

            Console.WriteLine("\nDisplay Managers\n");
            mary.DisplayStats();
            Console.WriteLine();
            chucky.RemoveReport(sam); // Remove a report for testing
            chucky.DisplayStats();
            Console.Write("Reports by Name: ");
            foreach (Employee emp in chucky.ReportsByName())
            {
                Console.Write("{0} ", emp.Name);
            }
            Console.Write("\nReports by Age: ");
            foreach (Employee emp in chucky.ReportsByAge())
            {
                Console.Write("{0}-{1} ", emp.Age, emp.Name);
            }
            Console.Write("\nReports by Pay: ");
            foreach (Employee emp in chucky.ReportsByPay())
            {
                Console.Write("{0:C}-{1} ", emp.Pay, emp.Name);
            }
            Console.WriteLine();

            Console.ReadLine();
        }
Exemplo n.º 29
0
        public CompDetails(Object data) : this()
        {
            this.DataContext = data;

            if (data is Employee)
            {
                string name  = "";
                string value = "";

                if (data is Manager)
                {
                    Manager man = (Manager)data;

                    name = "Reports";
                    string options = "Stock Options";

                    // Set spare prop name and value
                    man.GetSpareProp1(ref name, ref value);
                    this.SpareProp1Name.Content  = name;
                    this.SpareProp1Value.Content = man.ReportsByName();

                    this.SpareProp2Name.Content  = options;
                    this.SpareProp2Value.Content = man.StockOptions;

                    // Make visible after setting values
                    this.SpareProp1Name.Visibility  = Visibility.Visible;
                    this.SpareProp1Value.Visibility = Visibility.Visible;

                    this.SpareProp2Name.Visibility  = Visibility.Visible;
                    this.SpareProp2Value.Visibility = Visibility.Visible;
                }
                if (data is Executive)
                {
                    Executive exe = (Executive)data;

                    name = "Reports";
                    string options = "Stock Options ";

                    // Set spare prop name and value
                    exe.GetSpareProp1(ref name, ref value);
                    this.SpareProp1Name.Content  = name;
                    this.SpareProp1Value.Content = exe.ReportsByName();

                    this.SpareProp2Name.Content  = options;
                    this.SpareProp2Value.Content = exe.StockOptions;

                    // Make visible after setting values
                    this.SpareProp1Name.Visibility  = Visibility.Visible;
                    this.SpareProp1Value.Visibility = Visibility.Visible;

                    this.SpareProp2Name.Visibility  = Visibility.Visible;
                    this.SpareProp2Value.Visibility = Visibility.Visible;
                }
                if (data is Engineer)
                {
                    Engineer eng = (Engineer)data;
                    name = "Highest Degree";

                    // Set spare prop name and value
                    eng.GetSpareProp1(ref name, ref value);
                    this.SpareProp1Name.Content  = name;
                    this.SpareProp1Value.Content = eng.HighestDegree;

                    // Make visible after setting values
                    this.SpareProp1Name.Visibility  = Visibility.Visible;
                    this.SpareProp1Value.Visibility = Visibility.Visible;
                }
                if (data is SalesPerson)
                {
                    SalesPerson sales = (SalesPerson)data;
                    name = "Number of Sales";

                    // Set spare prop name and value
                    sales.GetSpareProp1(ref name, ref value);
                    this.SpareProp1Name.Content  = name;
                    this.SpareProp1Value.Content = sales.SalesNumber;

                    // Make visible after setting values
                    this.SpareProp1Name.Visibility  = Visibility.Visible;
                    this.SpareProp1Value.Visibility = Visibility.Visible;
                }
                if (data is PTSalesPerson)
                {
                    PTSalesPerson pTSales = (PTSalesPerson)data;
                    name = "Number of Sales";

                    // Set spare prop name and value
                    pTSales.GetSpareProp1(ref name, ref value);
                    this.SpareProp1Name.Content  = name;
                    this.SpareProp1Value.Content = pTSales.SalesNumber;

                    // Make visible after setting values
                    this.SpareProp1Name.Visibility  = Visibility.Visible;
                    this.SpareProp1Value.Visibility = Visibility.Visible;
                }
                if (data is SupportPerson)
                {
                    SupportPerson support = (SupportPerson)data;
                    name = "Shift: ";

                    // Set spare prop name and value
                    support.GetSpareProp1(ref name, ref value);
                    this.SpareProp1Name.Content  = name;
                    this.SpareProp1Value.Content = support.Shift;

                    // Make visible after setting values
                    this.SpareProp1Name.Visibility  = Visibility.Visible;
                    this.SpareProp1Value.Visibility = Visibility.Visible;
                }
            }
        }