How to popular a parameter property with GET access

Asked

Viewed 95 times

4

To understand what I want to do, note the Formclosing() parameter of Windowsforms:

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     var r = e.CloseReason;
 }

By analyzing this method, I cannot modify the Closereason property within the method, just receive. But how is this property populated by values, and its access is GET? How does this method work internally?

I am trying to copy the same reasoning of the above method, but I am not getting it. The way I am doing below, is giving error.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Modify modify = new Modify();
        modify.Refresh += Modify_Refresh;
        modify.Start();
    }

    // esse é o método que está seguindo a mesma linha de raciocínio do método acima
    private void Modify_Refresh(ValueArgs e)
    {
        var r = e.IsRunning;
    }
}

public class Modify
{
    public delegate void EventRefresh(ValueArgs e);
    public event EventRefresh Refresh;
    public void Start()
    {
        ValueArgs valueArgs = new ValueArgs();
        valueArgs.IsRunning = true;
        Refresh(valueArgs);
    }
}

public class ValueArgs
{
    public bool IsRunning { get; }
}

Note that when I set Isrunning to TRUE, it gives error saying that the property is not assignable, but read-only. How do I make this code work the right way, following the same line of reasoning as the Formclosing() method of Windows Form?

1 answer

3


(...) where your access is GET? How does this method work internally?

Instead of declaring the property in the "self-applied" form, implement it using a private field(backing field).

Unlike the "self-applied" property, which uses a blacking field implicit, the traditional form, blacking field explicitly, allows this to be "set" without the need to declare a set to the property.

That’s the way the class Formclosingeventargs implements the property CloseReason. A private field is used(backing field) where is stored the Closereason which is passed to the builder of Formclosingeventargs.
The implementation of get, of the property, returns the value of the backing field.

public class FormClosingEventArgs : CancelEventArgs {
    private CloseReason closeReason;

    public FormClosingEventArgs(CloseReason closeReason, bool cancel)
    : base(cancel) {
        this.closeReason = closeReason;                                           
    }

    public CloseReason CloseReason {
        get {
            return closeReason;
        }
    }
}

Applying to the question code would be something like this:

public class Modify
{
    public delegate void EventRefresh(ValueArgs e);
    public event EventRefresh Refresh;
    public void Start()
    {
        ValueArgs valueArgs = new ValueArgs(true);
        Refresh(valueArgs);
    }
}

public class ValueArgs
{
    private isRunning;

    public ValueArgs(bool isRunning)
    {
        this.isRunning = isRunning;
    }

    public bool IsRunning
    {
        get
        {
            return isRunning;
        }
    }
}

Another way is to create the Modify and Valueargs classes in another Assembly and declare how internal the set of property IsRunning:

public class ValueArgs
{
    public bool IsRunning { get; internal set;}
}

Being the set internal, and the Modify class is in the same Assembly, he is accessible to her.

Browser other questions tagged

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