Пример #1
0
        /// <summary>
        /// Create an animal object of a species (Bee,Butterfly) of Insect cateogry.
        /// </summary>
        /// <param name="Species">Species: Bee, Butterfly, etc.</param>
        /// <returns>Object of the Species type.</returns>
        /// <remarks></remarks>
        public static Insect CreateInsect(InsectSpecies Species)
        {
            Insect animalObj = null;        //type not known at this time

            //type determined by late binding
            switch (Species)
            {
                case InsectSpecies.Bee:
                    animalObj = new Bee();           //Late binding
                    break;
              //Continue with the rest
                case InsectSpecies.Butterfly:
                    animalObj = new Butterfly();    //Late binding
                    break;

                default:
                    Debug.Assert(false, "To be completed!");
                    break;
            }

            //Set the category
            animalObj.Category = CategoryType.Insect;

            return animalObj;     //return the created animal object.
        }
Пример #2
0
        //instance variables
        public static Insect CreateInsect(InsectSpecies species)
        {
            //type not known at this time
            Insect animalObj = null;

            try
            {
                //type determined by late binding - species is an enumeration
                switch (species)
                {
                    case InsectSpecies.Bee:

                        //ID, nickName, age, category, gender
                        animalObj = new Bee(); //late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }
                        break;

                    //continue with the rest
                    case InsectSpecies.Butterfly:

                        animalObj = new Butterfly(); //late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }
                        break;

                    default:
                        Debug.Assert(false, "To be completed!");
                        break;

                }
            }
            //custom exception
            catch (Exception e)
            {
                e.Message.ToString();
                NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                throw (ex);
            }
            finally
            {

                //Set the category
                animalObj.Category = CategoryType.Insect;
            }
            return animalObj;
        }
Пример #3
0
        //instance variables

        public static Insect CreateInsect(InsectSpecies species)
        {
            //type not known at this time
            Insect animalObj = null;

            try
            {
                //type determined by late binding - species is an enumeration
                switch (species)
                {
                case InsectSpecies.Bee:

                    //ID, nickName, age, category, gender
                    animalObj = new Bee();     //late binding

                    if (animalObj == null)
                    {
                        NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                        throw (ex);
                    }
                    break;

                //continue with the rest
                case InsectSpecies.Butterfly:

                    animalObj = new Butterfly();     //late binding

                    if (animalObj == null)
                    {
                        NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                        throw (ex);
                    }
                    break;

                default:
                    Debug.Assert(false, "To be completed!");
                    break;
                }
            }
            //custom exception
            catch (Exception e)
            {
                e.Message.ToString();
                NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                throw (ex);
            }
            finally
            {
                //Set the category
                animalObj.Category = CategoryType.Insect;
            }
            return(animalObj);
        }