Doubt how to send calling a method in another class

Asked

Viewed 573 times

0

I have a cl_Calculo class that has a method that calls a table in sql by a CRUD class already created, and I want in a Form when clicking a button to call the method of this class passing the sql command and that presents the result in a Message box.

namespace Calculos {
  public class cl_Calculos {

    public int[] Dezenas(int jogo, string query) {

      #
      region Apresenta as Dezenas do concurso consultado
      cl_GestorDB gestor = new cl_GestorDB();
      //string query = "SELECT * FROM dbo.seq_concurso WHERE concurso = " + jogo;            
      DataTable dados = gestor.EXE_READER(query + "'" + jogo + "'");
      //-------------------------------------------            
      int dezena_1 = Convert.ToInt32(dados.Rows[0]["01"].ToString());
      int dezena_2 = Convert.ToInt32(dados.Rows[0]["02"].ToString());
      int dezena_3 = Convert.ToInt32(dados.Rows[0]["03"].ToString());

      int[] dezenas = new int[] {
        dezena_1,
        dezena_2,
        dezena_3,
      }; //Cria um array com as dezenas sorteadas
      #
      endregion
      //--------------------------------------------------------------            
      return dezenas;
    }

    namespace WindowsFormsApp7 {
      public partial class Form1: Form {
        private void btn_Teste_Click(object sender, EventArgs e) {

          cl_Calculos Jogo = new cl_Calculos(10, "SELECT* FROM dbo.seq_concurso WHERE concurso = ");

          string toDisplay = string.Join(Environment.NewLine, Jogo.Dezenas);
          MessageBox.Show(toDisplay);

        }
        //==================================================================

2 answers

3

I don’t know if it would be the best solution, but you could create a method that takes the connection string as parameter and returns the string, passing it to Messagebox.

Creating the method:

public string Resultado(string strQuery){
    _Calculos Jogo = new cl_Calculos();
    return string.Join(Environment.NewLine, Jogo.Dezenas(10, strQuery);
}

In the Click() button event, you could call by passing the method as parameter to Messagebox:

private void btn_Teste_Click(object sender, EventArgs e) {
    MessageBox.Show(Resultado("SELECT* FROM dbo.seq_concurso WHERE concurso = "));
}

Using this method, you can change the query string when necessary. If you want, you can add an int in the method parameters to inform the number of tens as well, it would be more or less like this:

public string Resultado(int numDezenas, string strConsulta){
    _Calculos Jogo = new cl_Calculos();
    return string.Join(Environment.NewLine, Jogo.Dezenas(numDezenas, strQuery);
}

2


namespace WindowsFormsApp7 { 

    public partial class Form1 : Form { 

        private void btn_Teste_Click(object sender, EventArgs e) {

            _Calculos Jogo = new cl_Calculos();

            string toDisplay = string.Join(Environment.NewLine, 
                   Jogo.Dezenas(10, 
                    "SELECT* FROM dbo.seq_concurso WHERE concurso = ");

            MessageBox.Show(toDisplay);

        }
    }
}

Browser other questions tagged

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