Skip to content
Advertisement

Method returns a value but the property does not hold it

The method LocalizaArquivo() calls the method VerificaArquivoCarregado() that checks if the file is already loaded. If it is loaded returns arquivoCarregado = true.

When I debug it, I can see that arquivoCarregado = true, but after the method VerificaArquivoCarregado() is finished it changes to false.

The property “public bool arquivoCarregado { get; set; }” does not mantain the status. Why that?

class Arquivo
{
        public bool arquivoCarregado { get; set; }

        public void LocalizaArquivo()
        {
            List<Senha> senhas = CEPSOUtils.CapturarSenha(Constantes.idSisasc, Constantes.tipoSenha);
            foreach (Senha senha in senhas)
            {
                using (Impersonar imp = new Impersonar(senha.login, Constantes.Domain, senha.senha))
                {
                    List<string> lsArquivos = new List<string>(Directory.GetFileSystemEntries(Constantes.PastaOrigem));
                    arquivoContratosSIOSP = lsArquivos.Where(a => a.Contains(Constantes.NomeArquivo)).Last().Split('\').Last();
                    VerificaArquivoCarregado(arquivoContratosSIOSP);
                }
            }
        }

        public bool VerificaArquivoCarregado(string arquitoTermoSICID)
        {
            bool arquivoCarregado = true;

            using (SqlConnection connSQL = new SqlConnection(Constantes.Conexao))
            {
                connSQL.Open();

                using (SqlCommand cmd = new SqlCommand(Constantes.ProcedureSIOSP01, connSQL))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandTimeout = 3600;
                    cmd.Parameters.AddWithValue(Constantes.no_arquivo, arquivoContratosSIOSP);

                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        //Se tiver linhas significa que o arquivo já foi carregado (select trouxe linha)
                        arquivoCarregado = dr.HasRows;
                        dr.Close();
                    }
                }
                connSQL.Close();
            }
            return arquivoCarregado;
        }

}

Advertisement

Answer

Your method creates a different variable with the same name and changes it’s value, but that variable goes out of scope when the method ends. You’re doing the right thing by returning the value from the method, but you’re not using the return value when you call the method.

Also, in c#, public properties should be PascalCase:

public bool ArquivoCarregado { get; set; }

There are a couple of ways to fix this:

  1. Don’t use a local variable and just set the value of the class property from the method (and replace the return type with void):
public void VerificaArquivoCarregado(string arquitoTermoSICID)
{
    using (SqlConnection connSQL = new SqlConnection(Constantes.Conexao))
    {
        connSQL.Open();

        using (SqlCommand cmd = new SqlCommand(Constantes.ProcedureSIOSP01, connSQL))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 3600;
            cmd.Parameters.AddWithValue(Constantes.no_arquivo, arquivoContratosSIOSP);

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                // Se tiver linhas significa que o arquivo 
                // já foi carregado (select trouxe linha)
                this.ArquivoCarregado = dr.HasRows;
            }
        }
    }
}
  1. Use the return value of the method to update the class property:
this.ArquivoCarregado = VerificaArquivoCarregado(arquivoContratosSIOSP);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement