1

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.

5
  • 1
    Create List of Type List<object> Commented May 16, 2013 at 8:31
  • 1
    The line GenericRecommendationForm oRecommendation= new GenericRecommendationForm (); will always result in the last else if to be executed. I assume that line is just for illustration purposes and doesn't exist in your real code? Commented May 16, 2013 at 8:32
  • 1
    dictionary.reference.com/browse/inherit Commented May 16, 2013 at 8:34
  • You are right, I forgot the else of the second if. Thanks for catching it. Commented May 16, 2013 at 8:36
  • You can't have a list of classes you can only have list of objects or list of object types. Commented May 16, 2013 at 8:37

2 Answers 2

4

The easiest way to achieve this is to give your GenericRecommendationForm a virtual Clone method that all derived forms override.

public class GenericRecommendationForm 
{
    // ...

    public virtual GenericRecommendationForm Clone()
    {
        return new GenericRecommendationForm(this);
    }
}

public class TypeARecommendationForm
{
    // ...

    public override GenericRecommendationForm Clone()
    {
        return new TypeARecommendationForm(this);
    }
}

public class TypeBRecommendationForm
{
    // ...

    public override GenericRecommendationForm Clone()
    {
        return new TypeBRecommendationForm(this);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can use a List<Type> to store your classes (well the types of your classes actually).

when you'll need to add another class, just use this:

List<Type> _classes = new List<Type>();
_classes.Add(typeof(YourClass));

Then when you test the type of your class:

foreach(Type t in _classes)
{
    if (oRecommendation.GetType() == t)
        recommendationForm = Activator.CreateInstance(t, new object[] { oRecommendation });
}

But if I were you I would consider implementing it in another way.

Since you seem to actually clone the instance of your base class, just add a virtual (or abstract) Clone method to your base class, and then add the behavior you need in your derived classes.

That way you won't have to check for types at all, and can just do:

oRecommendation.Clone()

2 Comments

You were right, the virtual clone() worked perfectly. Thanks for your help.
you're welcome ;), since Daniel's answer is more complete about the Clone method you should mark his answer as accepted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.