Check when global variable changes

2

I have the following problem, I have a global variable public static bool HouveAlteracaoBD { get; set; } and I need to monitor it when there is a change in its value, since I want to follow the following logic:

if(Houve alteração na variável global "HouveAlteracaoBD" == true)
{
    CadEmpresaEntity cadEmpresa = new CadEmpresaFacade().GetCadEmpresa(empresaId, ref resultado);
    cadEmpresa.Ativa = true;
    new CadEmpresaFacade().Alterar(cadEmpresa, ref resultado);
}

My problem is in the expression of if, I need to wait for the global variable to undergo a change, but I do not know how to do that, if anyone can help I thank.

    
asked by anonymous 20.07.2017 / 16:28

3 answers

2

You can take advantage of the set accessor of the property. Save the real value of the property in a separate field and use the property to encapsulate its access, like this:

public class Foo
{
    private static bool houveAlteracao;

    public static bool HouveAlteracao
    {
        get { return houveAlteracao; }
        set
        {
            if (!houveAlteracao && value) // valor alterado para true
            {
                // lógica
            }
            else if (houveAlteracao && !value) // valor alterado para false
            {
                // lógica, caso deseje fazer algo nesse caso também.
            }

            houveAlteracao = value;
        }
    }
}

As a logic you can use the body of if you showed in the question.

    
20.07.2017 / 17:04
2

There is a specific interface for this - INotifyPropertyChanged that has a PropertyChanged member which is an event that we can subscribe to.

public class MyClass : INotifyPropertyChanged
{
    private string imageFullPath;

    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, e);
    }

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    public string ImageFullPath
    {
        get { return imageFullPath; }
        set
        {
            if (value != imageFullPath)
            {
                imageFullPath = value;
                OnPropertyChanged("ImageFullPath");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Reference: [Link]

    
20.07.2017 / 17:11
1

As a seemingly simple case, you can set this in the set method of the property.

public class SuaClasse
{
    private bool houveAlteracaoBd;

    public bool HouveAlteracaoBD
    {
        get { return houveAlteracaoBd; }
        set
        {
            if(value != houveAlteracaoBd)
            {
                houveAlteracaoBd = value;

                if(value) //Se for true
                    HouveAlteracao();
            }
        }
    }

    private void HouveAlteracao()
    {
        CadEmpresaEntity cadEmpresa = new CadEmpresaFacade().GetCadEmpresa(empresaId, ref resultado);
        cadEmpresa.Ativa = true;
        new CadEmpresaFacade().Alterar(cadEmpresa, ref resultado);
    }
}
    
20.07.2017 / 16:55