Access a Form method to another Form

Asked

Viewed 109 times

1

I have a Method carregarLista() on form calling Funcionarios, and would like to access this method from another Form called Cancelar. The method is published in Form Funcionarios, I just can’t access it from Cancelar.

public void carregarLista()
    {

        comboList.Items.Clear();
        sqlCommand.Connection  =  conexão.Conectar();
        sqlCommand.CommandText = "SELECT *FROM CadFuncionário";
        dr = sqlCommand.ExecuteReader();

        if (dr.HasRows)
        {
            while (dr.Read())
            {

                comboList.Items.Add(dr[2].ToString());

            }
        }
        conexão.Desconectar();
        txtNome.Clear();
    }

I made the Form Employees instance inside the Cancel Form only I still don’t have access to the method.

Funcionarios funcionarios = new Funcionarios();
  • Just set it as public or international.

  • I hadn’t read that you’re already in public, that’s bad. You can reproduce this in a minimal example and post the code?

2 answers

1

The big structural problem is that this has a responsibility which is to organize the data that is taken from the database, at least the bulk of the code is this. What escapes this responsibility is the comboList.Items.Add(dr[2].ToString());.

This code should be part of another object that takes care of the model and not the vision (the UI). If it were separated, it could call from wherever it wants. It is not simple to do this right, to have performance would have to use advanced mechanisms that many do not know or master, but it is correct.

This way that you’re doing is the same way many people are doing and in something simple it’s going well, but it’s gambiarra, it always has been, even if almost everyone does so.

If you still want to insist on the gambiarra and make everything worse, but that it works (I am explicitly not recommending doing this, but I know you will), you can parametrize this method to be used in a more generic way. This is if the method actually does exactly what you want without changing anything. This parameter would be the comboList that you would always pass, in this form or the other. You cannot use without the parameter because in this current method the comboList in fact is the this.comboList and you don’t want that if you’re going to use it in another form.

I’d still have to see what to do with the txtNome that has the same problem, should you parameterize it too? Probably yes.

I have already said that calling a method a form, and that seems to be implementation detail and so private, so making it public is already another mistake, and that all this is gambiarra da grossa?

To tell the truth is less gambiarra copy and paste this code there in the other form. It is not that good, but cause less problem.

This code has other problems and leaks feature in some situations, although it is not noticeable in most of them. This pattern of reinventing opening connection was taught by someone one day and everyone went wrong.

0

A simple way to do this is to create a static instance of employees in the employee script itself like this:

class Funcionarios : Form
{
    //Passo 1: Declarar váriavel public e static:
    public static Funcionarios funInstancia;

    public Funcionarios()
    {
        InitializeComponent();

        //Passo 2: Atribuir this (o objeto/formulário) para funInstancia
        funInstancia = this;
    }

    public void CarregarLista()
    {
        //etc etc...
    }
}

And then just access the Static variable of the Employees class from anywhere like this:

//Para acessar a variável/objeto
Funcionarios.funInstancia. // .qualquer método, objeto, controle, etc que for público

//Para acessar o método "CarregarLista"
Funcionarios.funInstancia.CarregarLista();
  • Okay, this may be a bit of a scam but FOR NOW.

Browser other questions tagged

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