Virtual method with Generic C#

Asked

Viewed 159 times

0

I am creating a Base Form where I am not using it of type "Generic" due to problem with inheritance of forms in C# with the same.

In this base form, I had the need to create a virtual method with "Generic", as it will be overwritten in the daughter class.

By overriding the method in the child class and "Swap" Generic for the class I am implementing I come across the following compilation error.

Error 1 'Project.ERP.Desktop.FormularioEstado.Get registrardpopulargridview(System.Linq.Expressions.Expression>)': no suitable method found to override C:Users Nicola Bogar Documents visual studio 2013 Projects Projeto.ERP.Solution Projeto.ERP.Desktop Formularioestado.Cs 39 38 Projeto.ERP.Desktop

Parent Form

public partial class FormularioBase : Form
{

    public virtual List<object> ObterRegistrosParaPopularGridView<TEntity>(Expression<Func<TEntity, bool>> where = null)
    {
        throw new NotImplementedException("Método ObterRegistrosParaPopularGridView da classe FormularioBase não implentada.");
    }
}

Formulário Filho

public partial class FormularioEstado : FormularioBase
{
    public override List<object> ObterRegistrosParaPopularGridView<Estado>(Expression<Func<Estado, bool>> where = null)
    {
        using (ProjetoContext contexto = new ProjetoContext())
        {
            if (where == null)
            {
                return contexto.Estados.Include(x => x.Pais)
                    .Select(x => new
                    {
                        Handle = x.Handle,
                        Descricao = x.Descricao,
                        Sigla = x.Sigla,
                        Pais = x.Pais,
                        PaisHandle = x.PaisHandle,
                        PaisDescricao = x.Pais.Descricao,
                        DataCadastro = x.DataCadastro,
                        DataAlteracao = x.DataAlteracao
                    }).ToList<object>();
            }
            else
            {
                return contexto.Estados
                    .Where(where)
                    .Select(x => new
                    {
                        Handle = x.Handle,
                        Descricao = x.Descricao,
                        Sigla = x.Sigla,
                        Pais = x.Pais,
                        PaisHandle = x.PaisHandle,
                        PaisDescricao = x.Pais.Descricao,
                        DataCadastro = x.DataCadastro,
                        DataAlteracao = x.DataAlteracao
                    }).ToList<object>();
            }

        }
    }
}

1 answer

0

Create the generic method, minimal example:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Class1
{
    public virtual List<T>
           ObterRegistrosParaPopularGridView<T>(Expression<Func<T, bool>> where = null)
    {
        throw new NotImplementedException("---");
    }
}

public class Class2: Class1
{
    public override List<User> 
     ObterRegistrosParaPopularGridView<User>(Expression<Func<User, bool>> where = null)
    {
        return base.ObterRegistrosParaPopularGridView(where);
    }
}

Your code would look something like this:

public partial class FormularioBase : Form
{
    public virtual List<T>
     ObterRegistrosParaPopularGridView<T>(Expression<Func<T, bool>> where = null)
    {
        throw new NotImplementedException("Método ObterRegistrosParaPopularGridView ...");
    }
}

Rewritten method:

public override List<Estado> 
 ObterRegistrosParaPopularGridView<Estado>(Expression<Func<Estado, bool>> where = null)
{
    return base.ObterRegistrosParaPopularGridView(where);
}
  • Virgilio Novic, my return to the list has to be an Object, because I am using the entityframework and I am performing John in tables, that is, the return is not the same class that I am performing the Where, I believe that is not the problem, because I created the same way you sent less return as Generic, but I think C# has some contract that forces the override method to be the same as the one defined in the FATHER class.

  • I ended up editing the post because I forgot to put <Status> in the method I’m doing the right override. But still the problem persists. I did a test here and when I’m doing it the way you told me to do it, Get registrosparapopulargridview<Status>(Expression<Func<Status, bool>> Where = null) The <Status> that is well ahead of the method C# recognizes as Generic, So he doesn’t really find the class I’m trying to use, you know ? That’s the problem.. =/

  • Okay, Perai, I’ll edit the post.. Momentoo.

  • See if editing helps anything buddy.

  • I agree with you on this issue, but is that this select is only for popular grid, I only use the object to perform Where in the entity I am working on.

Browser other questions tagged

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