Natu Lauchande
Natu Lauchande

Reputation:

How to initialize a derived class in C#?

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

Answers (5)

Jon Skeet
Jon Skeet

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:

  • You can use base(arguments) or this(arguments) - the first version calls a base class constructor, the second calls another constructor in the same class
  • If you don't specify anything, it's like having base()
  • You can't make the constructor calls circular (i.e. one constructor calling another which calls the first)
  • You can only chain to one constructor (but that one may chain to another)
  • The constructor call occurs immediately before the constructor body is executed, but after any instance variable initializers

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

n8wrl
n8wrl

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

Jake Pearson
Jake Pearson

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

eulerfx
eulerfx

Reputation: 37719

public  class RegistarAnuncio:Processor
{
    public RegistarAnuncio(ParsedString parsedMessage, Utilizador utilizador)
      : base(parsedMessage, utilizador)
    {
    }
}

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564433

You need to do:

public RegistarAnuncio(ParsedString parsedMessage, Utilizador utilizador)
    : base(parsedMessage, utilizador)
{
}

Upvotes: 15

Related Questions