Reputation:
I have a base class:
public class Processor
{
ParsedString _parsedMessage;
Utilizador _utilizador;
public Processor(ParsedString parsedMessage, Utilizador utilizador)
{
_parsedMessage = parsedMessage;
_utilizador = utilizador;
}
public virtual String Execute()
{
string result = null;
return result;
}
}
then a derived class
public class RegistarAnuncio:Processor
{
public RegistarAnuncio(ParsedString parsedMessage, Utilizador utilizador)
{
}
}
and the compiler is giving an error saying:
Error 9 No overload for method 'Processor' takes '0' arguments C:\Documents and Settings\user\My Documents\projectos\Licenciatura\Prototipo\Agrinfo\AgrinfoLib\Interfaces\SMS\Processors\RegistarAnuncio.cs 11 16 AgrinfoLib
I googled for C# reference but i did not found a code example where people initialize a base class method with arguments, can anyone give me an help.
Regards,
Upvotes: 1
Views: 6516
Reputation: 1500695
You want to use this syntax to call the base constructor:
public class RegistarAnuncio : Processor
{
public RegistarAnuncio(ParsedString parsedMessage, Utilizador utilizador)
: base (parsedMessage, utilizador)
{
}
}
I have an article on constructors which goes into more detail. Very briefly:
base(arguments)
or this(arguments)
- the first version calls a base class constructor, the second calls another constructor in the same classbase()
One common use for "this
" is to use default values. For instance:
const string DefaultFirstValue = "fred";
const int DefaultSecondValue = 20;
public Foo(string firstValue, int secondValue)
{
this.firstValue = firstValue;
this.secondValue = secondValue;
}
public Foo(string firstValue) : this(firstValue, DefaultSecondValue)
{
}
public Foo(int secondValue) : this(DefaultFirstValue, secondValue)
{
}
public Foo() : this(DefaultFirstValue, DefaultSecondValue)
{
}
With C# 4, this will be less useful as there will be optional parameters and named arguments. You may still wish to provide the overloads for languages which don't support those features, of course.
Upvotes: 20
Reputation: 19765
You have to call the base class' constrcutor from your derived class. The error is complaining becuase you do not have a default parameter-less constructor in yoru base class. Try this:
public class RegistarAnuncio:Processor
{
public RegistarAnuncio(ParsedString parsedMessage, Utilizador utilizador)
: base(parsedMessage, utilizador)
{
}
}
Upvotes: 2
Reputation: 27717
You need to call an existing constructor of the base class
public class RegistarAnuncio:Processor
{
public RegistarAnuncio(ParsedString parsedMessage, Utilizador utilizador)
: base(parsedMessage, utilizador)
{
}
}
Upvotes: 3
Reputation: 37719
public class RegistarAnuncio:Processor
{
public RegistarAnuncio(ParsedString parsedMessage, Utilizador utilizador)
: base(parsedMessage, utilizador)
{
}
}
Upvotes: 2
Reputation: 564433
You need to do:
public RegistarAnuncio(ParsedString parsedMessage, Utilizador utilizador)
: base(parsedMessage, utilizador)
{
}
Upvotes: 15