I am trying to improve a piece of code that I wrote but I need your help.
Basically, I have 2 classes TypeARecommendationForm and TypeBRecommendationForm that are inherited from GenericRecommendationForm and I am trying to copy the object oRecommendation in a new object.
For that I am checking the type to see which class to use.
The problem is that sometimes I create new classes that inherit from GenericRecommendationForm and I have to go through the all codes to see where to add these if statements as I have them at several places.
What I would like is to have like a list of the classes so that when I add a new classes I just need to add it to the list.
Does anyone know of a way to do it?
Here is the code:
GenericRecommendationForm oRecommendation= new GenericRecommendationForm ();
...
if (oRecommendation.GetType() == typeof(TypeARecommendationForm))
{
recommendationForm = new TypeARecommendationForm((TypeARecommendationForm)oRecommendation);
}
if (oRecommendation.GetType() == typeof(TypeBRecommendationForm))
{
recommendationForm = new TypeBRecommendationForm((TypeBRecommendationForm)oRecommendation);
}
else if (oRecommendation.GetType() == typeof(GenericRecommendationForm))
{
recommendationForm = new GenericRecommendationForm(oRecommendation);
}
Thanks in advance for your help.
List<object>GenericRecommendationForm oRecommendation= new GenericRecommendationForm ();will always result in the lastelse ifto be executed. I assume that line is just for illustration purposes and doesn't exist in your real code?