Help with parameters TextBox / string

3

I have this method where I would like to bring IBGE. When I put the TextBox as a parameter, it usually comes up, but the problem is that I am using a Library and can not import the TextBox. So I replace the TextBox with a string, but that way it can not bring ... Can someone help me?

public bool recuperarIBGE(string Cidade, string IBGE)
    {
        conexao = new Conexao();

        try
        {
            conexao.conectar();
            conexao.abrirConexao();

            NpgsqlCommand sql = new NpgsqlCommand("SELECT codibge FROM cidade WHERE nome ilike SEM_ACENTO('" + Cidade + "');", conexao.conexao);

            NpgsqlDataReader dr = sql.ExecuteReader();

            while (dr.Read())
            {
                //IBGE.Text = (string)dr["codibge"].ToString();
                IBGE = (string)dr["codibge"].ToString();
                retorno = true;

            }
            conexao.fecharConexao();
        }
        catch (Exception erro)
        {
            throw erro;
        }

        return retorno;
    }
    
asked by anonymous 29.07.2015 / 13:19

2 answers

1

In order for the change in the value in the IBGE parameter to be visible in the code that called the method you must set this parameter to out .

Simply change the method signature to

public bool recuperarIBGE(string Cidade, out string IBGE)

and be sure to assign a value to all method's branch branches.

At the time of calling the method you also need to pass the out modifier in the IBGE parameter:

string ibge;

if (recuperarIBGE("Goiânia", out ibge))
{
    Console.WriteLine("O código IBGE da cidade é" + ibge);
}

In this way, after calling the recuperarIBGE method, the IBGE attribute will have the value that was assigned to it within the method.

    
29.07.2015 / 19:21
1

To do this, use the keyword ref or out

out (C # Reference)

ref (C # Reference)

    
29.07.2015 / 22:44