Show / Hide Table of Contents

Class PropertyChangedEventHelpers

Helper functions useful for implementing events using ValueChangedEventArgs<TProperty> as their parameter.

Inheritance
Object
PropertyChangedEventHelpers
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
public static class PropertyChangedEventHelpers : Object

Methods

SafelySetProperty<TObject, TProperty>(TObject, ref TProperty, TProperty, EventHandler<ValueChangedEventArgs<TProperty>>)

Sets the given field to the new value, and fires the corresponding event just after the value has been changed. The value will be properly reverted to the old value if the event handler throws InvalidOperationException.

Declaration
public static void SafelySetProperty<TObject, TProperty>(this TObject self, ref TProperty propertyField, TProperty newValue, EventHandler<ValueChangedEventArgs<TProperty>> changedEvent)
Parameters
Type Name Description
TObject self
TProperty propertyField

Field to set.

TProperty newValue

New value to set to given field.

EventHandler<ValueChangedEventArgs<TProperty>> changedEvent

Event to fire when change occurs.

Type Parameters
Name Description
TObject

Type of the object the property resides on.

TProperty

Type of the property.

Remarks

It is fairly common to have an object with a property, such that when the property changes, an event is fired. IPositionable represents one such interface that is common when dealing with a 2D grid.

Although the implementation of such a property is relatively trivial, there are a few subtle issues that can occur:

  • The event might be fired even if the property was set to the same value it is currently
  • If a handler throws something like InvalidOperationException, the value may not be reverted properly.

This function represents a convenient way to implement such a property. An implementation using this property might look like the example below.

Note that this function can work for properties of any type; you could just as easily implement IPositionable via this interface, for example.

class MyClass
{
    private int _myProperty;
    public int MyProperty
    {
        get => _myProperty;
        set => this.SafelySetProperty(ref _myProperty, value, MyPropertyChanged);
    }
    public event EventHandler<ValueChangedEventArgs<int>>? MyPropertyChanged;
}

SafelySetProperty<TObject, TProperty>(TObject, ref TProperty, TProperty, EventHandler<ValueChangedEventArgs<TProperty>>, EventHandler<ValueChangedEventArgs<TProperty>>)

Sets the given field to the new value, and fires the corresponding events. The value will be properly reverted to the old value if the Changed event handler throws InvalidOperationException. The "changing" event will be fired just before the value actually changes; the "changed" event will be fired just after.

Declaration
public static void SafelySetProperty<TObject, TProperty>(this TObject self, ref TProperty propertyField, TProperty newValue, EventHandler<ValueChangedEventArgs<TProperty>> changingEvent, EventHandler<ValueChangedEventArgs<TProperty>> changedEvent)
Parameters
Type Name Description
TObject self
TProperty propertyField

Field to set.

TProperty newValue

New value to set to given field.

EventHandler<ValueChangedEventArgs<TProperty>> changingEvent

Event to fire when change is about to occur.

EventHandler<ValueChangedEventArgs<TProperty>> changedEvent

Event to fire after change occurs.

Type Parameters
Name Description
TObject

Type of the object the property resides on.

TProperty

Type of the property.

In This Article
Back to top Generated by DocFX