TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Chris Anderson
NA
93
9.3k
How can I prevent DRY for this method?
Jan 30 2021 12:35 PM
Having each scope almost the same like below doesn't feel right at all. I'd rather have one scope where name, age i.e. are set than having 3 for each object of cat, dog and puppy. How can this code below be improved?
private
void
addAnimal_Click(
object
sender, EventArgs e)
{
var name = inputName.Text;
int
age =
int
.Parse(inputAge.Text);
var favfood = inputFavfood.Text;
var breed = inputBreed.Text;
var typeOfAnimal = dropDownTypeOfAnimal.Text;
if
(typeOfAnimal ==
"Cat"
)
{
Animal cat =
new
Cat();
cat.Name = name;
cat.Age = age;
cat.FavFood = favfood;
cat.Breed = breed;
cat.IsHungry = utility.ShuffleProbability();
Pets.Add(cat);
}
if
(typeOfAnimal ==
"Dog"
)
{
Animal dog =
new
Dog();
dog.Name = name;
dog.Age = age;
dog.FavFood = favfood;
dog.Breed = breed;
dog.IsHungry = utility.ShuffleProbability();
Pets.Add(dog);
}
if
(typeOfAnimal ==
"Puppy"
)
{
Animal puppy =
new
Puppy();
puppy.Name = name;
puppy.Age = age;
puppy.FavFood = favfood;
puppy.Breed = breed;
puppy.IsHungry = utility.ShuffleProbability();
Pets.Add(puppy);
}
}
Reply
Answers (
4
)
Editing a cell in a selected datagrid view.
Trying to display