Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI
I have a class that inherits from another one but i get this confusing error :
VB
Error   1  'WindowDecorator.Decorator' does not contain a constructor that takes 0 arguments


Error occurs in the constructor.

C#
public class ScrollDecorator:Decorator
    {
           protected Decorator decorator;

           protected float ScrollPosition;


       protected ScrollDecorator(Decorator _decorator)
        {
            this.decorator = _decorator;
        }


       public  void ScrollTo()
       {
           //return "";
       }

       public override void Draw()
       {

       }
    }
Posted
Comments
[no name] 3-May-12 7:31am    
You are trying to instantiate an instance of the Decorator class that does not have a 0 argument constructor. What is so confusing about that?

The reason is that the base class Decorator does not contain a parameter less constructor i.e. a constructor which takes 0 arguments.

When the constructor of this derived class is called it will call the parameter less constructor of the base class as there is no explicit call to the constructor of the base class and since there is no parameter less constructor in the base class the above error occurs.

To avoid this error call the constructor of the base class with appropriate arguments as required for the constructor available in the base class as shown below:
C#
//Replace Arguments in base  with arguments as required for the 
//constructor of Decorator
protected ScrollDecorator(Decorator _decorator) : base(Arguments)
{
    this.decorator = _decorator;
}
 
Share this answer
 
Comments
Espen Harlinn 3-May-12 8:45am    
5'ed!
VJ Reddy 3-May-12 8:49am    
Thank you, Espen.
you missing default constructor
add following
protected ScrollDecorator()
      {

      }
 
Share this answer
 
Comments
Anele Ngqandu 3-May-12 7:05am    
Well Sir I tried that but still nothing thats why am confused by this
Prasad_Kulkarni 3-May-12 7:23am    
Countered
Sangramsingh Pawar 3-May-12 7:31am    
I think you missing to call base class constructor. It seems that base class has no default constructor, therefore u callbase class constructer that takes arguments
Anele Ngqandu 3-May-12 7:39am    
thats true Sangramsingh
Hello,

i think this error occurred because you create object of the class without passing a value like
C#
Test t = new Test();

and your class don't have 0 arguments constructor

Best Luck
Happy Coding
 
Share this answer
 
You are inheriting from a class and having the same class as instance member - wierd????

anyway looks like you base class has no default constructor and you will have to take the arguments that base class is expecting as derived class arguments and then pass them to the base class constructor as base(args) in derived class constructor.

C#
protected ScrollDecorator(Decorator _decorator, args) : base(args)
{
    this.decorator = _decorator;
}
 
Share this answer
 
type conflict

C#
public class ScrollDecorator:Decorator
    {
           protected Decorator decorator;

           protected float ScrollPosition;


       protected ScrollDecorator(Decorator _decorator)
        {
            this.decorator = _decorator;
        }


       public  void ScrollTo()
       {
           //return "";
       }

       public override void Draw()
       {

       }
    }

also add below constructor

C#
ScrollDecorator()
        {
            
        }
 
Share this answer
 
Comments
Anele Ngqandu 3-May-12 7:14am    
I have to do the inheritance so cant do that...

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