Skip to content
Loading
  • Rijwan Ansari
    +3
    In your case, I don't see any different properties based on if condition except object itself. 
     
    Simply,
    1. private void addAnimal_Click(object sender, EventArgs e)    
    2.         {    
    3.     
    4.             var name = inputName.Text;    
    5.             int age = int.Parse(inputAge.Text);    
    6.             var favfood = inputFavfood.Text;    
    7.             var breed = inputBreed.Text;    
    8.             var typeOfAnimal = dropDownTypeOfAnimal.Text;    
    9.             Animal animal = null;  
    10.     
    11.             if (typeOfAnimal == "Cat")    
    12.             {    
    13.                 animal = new Cat();     
    14.             }    
    15.             else if (typeOfAnimal == "Dog")    
    16.             {    
    17.                 animal = new Dog();     
    18.             }    
    19.             else if (typeOfAnimal == "Puppy")    
    20.             {    
    21.                 animal = new Puppy();    
    22.             }      
    23.                 animal.Name = name;    
    24.                 animal.Age = age;    
    25.                 animal.FavFood = favfood;    
    26.                 animal.Breed = breed;    
    27.                 animal.IsHungry = utility.ShuffleProbability();    
    28.                 Pets.Add(animal);           
    29.     
    30.         }  
     Addtionally, use if else or switch for better, as it will skip other conditions if one case is matched. 
     
    Regards 
    +3
  • Sachin Singh
    +3
    you can directly create the object based on string and in this way you will be free from if else.
    1.         
    2.   
    3. private void addAnimal_Click(object sender, EventArgs e)    
    4.         {    
    5.     
    6.             var name = inputName.Text;    
    7.             int age = int.Parse(inputAge.Text);    
    8.             var favfood = inputFavfood.Text;    
    9.             var breed = inputBreed.Text;    
    10.             var typeOfAnimal = dropDownTypeOfAnimal.Text;    
    11.     
    12.               
    13.               var assemblyName = "ConsoleTest"; // your namespace name  
    14.            
    15.                Type t = Type.GetType(assemblyName+"."+typeOfAnimal );  
    16.                 var obj= Activator.CreateInstance(t) as typeOfAnimal ;  
    17.     
    18.                 obj.Name = name;    
    19.                 obj.Age = age;    
    20.                 obj.FavFood = favfood;    
    21.                 obj.Breed = breed;    
    22.                 obj.IsHungry = utility.ShuffleProbability();    
    23.                 Pets.Add(cat);    
    24.            
    25.             
    26.             }              
    27.     
    28.         }   
    Note :- I have used Reflection.
               
    +3
  • Laxmidhar Sahoo
    +2
     hi
    No need to take three classes just use one common calss named like petanimal and a distingushed filed
    like pet type and add with the list 
    +2