Delegate specifics

I have a problem with a delegate in the class on a project I'm working on. A class is a GUI component that accepts both a label and a value. The idea here is that the user can specify a label, and then refer to a value from anywhere (more precisely, this is the ToString Method value), so every time this value is updated, the GUI component also. Here's how it works:

public delegate string GUIValue();

public class GUIComponent
{
    GUIValue value = null;    // The value linked in
    string label = "";        // The label for the value
    string text = "";         // The label and value appended together

    public GUIComponent(string Text, GUIValue Value)
    {
        this.text = Text;
        this.value += Value;
    }

    public void Update()
    {
        this.text = this.label + this.value();
    }
}

And then I call it that

GUIComponent component = new GUIComponent("Label: ",
                                new GUIValue(this.attribute.ToString));

The code compiles correctly, and the component displays and displays the initial value for this attribute, however, it does not update when the attribute value changes.

, , , . , return ToString, , - ?

+3
4

:

new GUIValue(this.attribute.ToString)

. , - "". - :

private event GUIValue attributeChanged = () => this.attribute.ToString();

private String attribute;

// This is a property that sets the value of attribute
public String Attribute { get { return attribute; } set { attribute = value; attributeChanged(); } }

// Now you can initialize the component using:
// GUIComponent component = new GUIComponent("Label: ", this.attributeChanged);
+1

.
value this.attribute.ToString.

, this.value() .

this.attribute, , , , , .
, , , , , update(), . , , .

, GUI - , .

+1

. , , , , GuiComponent , - , GUIValue, . , - , - , "", , .

This is how I would structure your code:

public interface IHaveAValueYouNeed 
{
    string ValueGetter();
    event EventArgs ValueChanged;
}

public class GUIComponent
{
    public delegate string ValueGetter();

    ValueGetter getter;    // The value linked in
    string label = "";        // The label for the value
    string text = "";         // The label and value appended together

    public GUIComponent(string Text, IHaveAValueYouNeed getter)
    {
        this.text = Text;
        this.getter += getter.ValueGetter;
        getter.ValueChanged += ValueUpdatedHandler;
    }

    public void Update()
    {
        this.text = this.label + this.value();
    }

    public void ValueUpdatedHandler(object sender, EventArgs e)
    {
        Update();
    }
}

Now, when you pass the implementation of the interface to the component, the component will exchange delegates with the instance, receiving a link to its ValueGetter and subscribing to its event. IHaveAValueYouNeed implementations should then raise an event when the value changes (either directly or because the computed value received by the getter has changed). Thus, the object controlling the meaning can tell people interested in the meaning that it has changed.

+1
source

Why not just use ToString?

public class GUIComponent
{
    object value = null;    // The value linked in
    string label = "";        // The label for the value
    string text = "";         // The label and value appended together

    public GUIComponent(string Text, object Value)
    {
        this.text = Text;
        this.value = Value;
    }

    public void Update()
    {
        this.text = this.label + this.value.ToString();
    }
}
+1
source

Source: https://habr.com/ru/post/1790050/


All Articles