Check when global variable changes

Asked

Viewed 620 times

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 any change in its value, because I intend 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 if expression, that I need to wait for the global variable to change, but I don’t know how to do that, if anyone can help I thank you.

  • Variable "global"? In what scope? Which class this variable belongs to?

  • It is in the same class as this excerpt I put above, but the code that changes its value is in another controller

3 answers

2


You can take advantage of the accessor set of the property. Store the real value of the property in a separate field and use the property to encapsulate your access, thus:

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 that showed in the question.

2

There is a specific interface for this - Inotifypropertychanged which has a member Propertychanged 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]https://stackoverflow.com/questions/2246777/raise--event-whenever-a-propertys-value-changed/2246837#2246837[/Link]

1

As it is a seemingly simple case, it is possible to define this in the method set of 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);
    }
}

Browser other questions tagged

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