Does not contain a constructor that takes 0 arguments

Asked

Viewed 350 times

0

My code is

   public partial class TelaInicio : MetroFramework.Forms.MetroForm 
   {

   internal ExibirDados exibirdados = null;

    public TelaInicio(ExibirDados exibirdados)
    {
        InitializeComponent();
        this.exibirdados = exibirdados;
    }

I’m trying to instantiate the form ExibirDados in form TelaInicio

class Exbirdados

public partial class ExibirDados : MetroFramework.Forms.MetroForm
{

    public ExibirDados()
    {
        InitializeComponent();
    }

}

And I get the following mistake

'C_buscadinamica.Telainicio' does not contain a constructor that takes 0 Arguments

The error itself is shown in the Program.cs

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        new SplashScreen().ShowDialog();
        Application.Run(new TelaInicio()); //aqui ocorre o erro
    }
}

How can I fix this? Why does this happen? Why do we need a constructor with zero arguments?

1 answer

2


You urged TelaInicio using a constructor that requires no arguments:

Application.Run(new TelaInicio()); //aqui ocorre o erro

But in the class declaration TelaInicio has been declared a single constructor that requires a class parameter ExibirDados:

public TelaInicio(ExibirDados exibirdados)
{
    InitializeComponent();
    this.exibirdados = exibirdados;
}

You have two options:

  1. Declare a constructor for class TelaInicio not requiring the passing of parameters:
public TelaInicio()
{
    InitializeComponent();
    this.exibirdados = new ExibirDados();
}

  1. Pass a class parameter ExibirDados for the constructor of the class TelaInicio:
Application.Run(new TelaInicio(new ExibirDados())); 
  • 2

    I removed my answer, yours is more complete :-) +1

Browser other questions tagged

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