Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi people,
I have very interesting question. initialization of derived class with its base class.
I mean there are two class named DerivedClass and BaseClass. DerivedClass derived by BaseClass. I want to do something like that

C#
class DerivedClass
{
   public DerivedClass(BaseClass bc)
   {
        base = bs;
   }
}


is that possible?
Posted
Updated 28-Jul-20 5:06am
Comments
[no name] 16-Oct-12 7:03am    
If you are quizing with us , my answer is : 'Why dont you try it your self'. And if you are testing the skills of people HELPing each other in that case I can only give you an clue : Read SOLID principle and use interface.
BillWoodruff 28-Jul-20 23:01pm    
This is a "crazy" question which confuses an instance of a Class with a Class definition. You need to get a good book and study C# basics.

No, I think you are confused.
C#
class DerivedClass
{
   public DerivedClass(BaseClass bc)
   {
        base = bs;
   }
}


Your "DerivedClass" is not related to "BaseClass" - except via Object which they both derive from. (Ignoring the typo "bs" / "bc")
You more likely want to say:
C#
class DerivedClass : BaseClass
{
   public DerivedClass(BaseClass bc)
   {
        base = bc;
   }
}
Which does derive from the base. But in that case, you don't need to create a DerivedClass instance from a Base Class instance, because Derived class is a type-of Baseclass - it already contains all the fields, properties, event and methods of the base class.

What you have there is basically a copy constructor (which you can do) but which should be irrelevant unless the rest of your system design is rather odd.
A better way to code it would be:
C#
class DerivedClass : BaseClass
    {
    public DerivedClass(BaseClass bc) : base (bc)
        {
        }
    }

But even then it's an odd idea.

What are you trying to do that you think you need this?
 
Share this answer
 
Comments
Mahmut Koyuncu 16-Oct-12 8:14am    
Thanks for response, okey MyBase class a graphic user interface I can lines curves in this class, but somewhere in my project I dont need graph only image of graph so i need some new class GraphShower And Class GraphDrawer is derived by Drawer class. GraphShower and GraphDrawer is in a communication each other. But in somewhere of project I need Drawer images and GraphDrawer and GraphShower do that automatically. İf Drawer class add this properties then Drawer class is out of its purpose. I dont want it so if i make sending Drawer type and copy it to base part of GraphDrawer in some way. BaseClass CopyConstructor is not defined by me, it is defined behind like Default constructor or it must be defined?
I think this is more relevant:

class DerivedClass : BaseClass
{
string someOtherInfo;
public DerivedClass(BaseClass bc)
{
base = bc;
}
}

I.e. initialize the BaseClass-ness of the Derived class field for field.

I am not sure if this can be done in C#. In C++ I think I could have done *(BasedClass)this = bc;

In C#, I think you might be able to do:

C#
class DerivedClass : BaseClass
{
   string someOtherInfo;
   public DerivedClass(BaseClass bc) : base(bc)
   {
   }
}


However, that requires that there is such a constructor in BaseClass.
 
Share this answer
 
v2
You can create a constructor in your derived class and map the objects or create an extension method like this:


public static class Extensions 
      {            
            public static void FillPropertiesFromBaseClass<T1, T2>(this T2 drivedClass, T1 baseClass) where T2 : T1
            {
                //Get the list of properties available in base class
                System.Reflection.PropertyInfo[] properties = typeof(T1).GetProperties();
    
                properties.ToList().ForEach(property =>
                {
                    //Check whether that property is present in derived class
                    System.Reflection.PropertyInfo isPresent = drivedClass.GetType().GetProperty(property.Name);
                    if (isPresent != null && property.CanWrite)
                    {
                        //If present get the value and map it
                        object value = baseClass.GetType().GetProperty(property.Name).GetValue(baseClass, null);
                        drivedClass.GetType().GetProperty(property.Name).SetValue(drivedClass, value, null);
                    }
                });
            }
      }


for example when you have to class like this:

public class Fruit {
        public float Sugar { get; set; }
        public int Size { get; set; }
    }
    public class Apple : Fruit {
        public int NumberOfWorms { get; set; }
    }




you can initialize derived class by this code:


//constructor 
    public Apple(Fruit fruit)
    {            
       this.FillPropertiesFromBaseClass(fruit);
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900