Question

I have a C# class called MyCustomRuleTemplate which is inherited from Ektron.Cms.Content.Targeting.Rules.RuleTemplate class. In that I have added a constructor such as below

public MyCustomRuleTemplate(): base("someKey")
{
//Some code here
}

Its working fine without any error. If I given it as

public MyCustomRuleTemplate()
{
//Some code here
}

Im getting error like 'Ektron.Cms.Content.Targeting.Rules.RuleTemplate' does not contain a constructor that takes 0 arguments. Can anybody help me to know why it is?

Was it helpful?

Solution

The reason you are seeing "does not contain a constructor that takes 0 arguments" when instantiating your class object using the second constructor is because when you call your constructor, c# tries to call the constructor on the base class as well, which in this case takes a parameter.

See this post on msdn:

http://msdn.microsoft.com/en-us/library/ms173115%28v=vs.80%29.aspx

Key parts:

"In this example, the constructor for the base class is called before the block for the constructor is executed. The base keyword can be used with or without parameters. Any parameters to the constructor can be used as parameters to base, or as part of an expression. For more information, see base.

In a derived class, if a base-class constructor is not called explicitly using the base keyword, then the default constructor, if there is one, is called implicitly."

And: "If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor using base."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top