Constructor class Abstract

Asked

Viewed 641 times

2

I’m failing to develop the following environment:

public abstract class AplicacaoGenerica<TEntity> where TEntity : class
{
    private IRepositorio<TEntity> repositorio;
    public AplicacaoGenerica(IRepositorio<TEntity> repo)
    {
        repositorio = repo;
    }

      (...)
}

Shows no error this class.

public class BandeiraAplicacao : AplicacaoGenerica<Bandeira>
{

}

In this class the following error appears:

does not contain a constructor that takes 0 Arguments

public class BandeiraAplicacaoConstrutor
{
    public static BandeiraAplicacao BandeiraAplicacaoEF()
    {
        return new BandeiraAplicacao(new BandeiraRepositorioEF());
    }
}

Displays the following error:

does not contain a constructor that takes 1 Arguments

I have an abstract class, Applicaogenerica, which has a constructor. So I create a class Bandeiraapplication that implements Applicaogenerica. Then the Application Class tries to call the Application Flag constructor.

1 answer

3


You have to create a constructor with the same structure in the sub class, something similar to this:

public class BandeiraAplicacao : AplicacaoGenerica<Bandeira>
{
     public BandeiraAplicacao(IRepositorio<TEntity> repo)
         // aqui estamos passando o argumento para a sub class 
         :base(repo)
     {
     }
}

The mistake you pointed out is saying that the class base has no empty constructor, for the class BandeiraAplicacao use in its construction.

So you have two options to solve this problem:

  1. Pass the required parameter to class base in the child builder (as in the example);
  2. Create an empty constructor on class groundwork;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.