Creating an Observable <T> Class: Overload = Operator?

I am trying to create a class that contains a value, will increment the event whenever the value changes, and will implicitly convert it to and from type.

My goal is so that I can create an Observable property of the class and be able to read and write another class (including WPF controls) as if it were a regular string. Other classes may maintain a reference to it as observable, or even expose it to their own property without the need to create new events.

Here is what I still have:

using System; using System.ComponentModel; namespace SATS.Utilities { public class Observable<T> { private T mValue; public event EventHandler ValueChanged; public event PropertyChangedEventHandler PropertyChanged; private void NotifyValueChanged() { if (ValueChanged != null) { ValueChanged(this, new EventArgs()); } if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Value")); } } public T Value { get { return mValue; } set { SetValueSilently(value); NotifyValueChanged(); } } public void SetValueSilently(T value) { mValue = value; } public static implicit operator T(Observable<T> observable) { return observable.Value; } public static T operator =(Observable<T> observable, T value) // Doesn't compile! { observable.Value = value; return value; } } } 

The problem is that the "=" operator complains that it cannot be overloaded. I guess this makes sense, as it can lead to all sorts of weird behaviors. Is there any other way to achieve what I'm going to do?

EDIT: This is how I decided to implement this. Let me know if there are any better suggestions :)

I realized that this case really had to be done using a property that contains an Observable. Here is an example of what I would like to do:

 public class A { private readonly Observable<string> _property; public Observable<string> Property { get { return _property; } } public string Property { set { _property.Value = value; } } } 

Of course, this does not compile because the property is defined twice. Here are a few hacky workarounds that I'm thinking of defining an implicit conversion in another way (as many of you have suggested):

 public static implicit operator Observable<T>(T value) { var result = new Observable<T>(); result.SetValueSilently(value); return result; } 

and using this to call the setter property:

 public Observable<string> Property { get { return _property; } set { _property.Value = value.Value; } } 
+7
source share
3 answers

You can overload the implicit statement.

 public static operator implicit string(YourObject object) 

and go the other way

 public static operator implicit YourObject(string s) 

Remember that this is very dangerous. This can lead to the fact that consumers in this class will do some things that you never thought about; and some kind of behavior that does not make sense.

+4
source

If you look at Overloaded Operators in C # , you will see that:

Assignment operators cannot be overloaded, but + =, for example, is evaluated using +, which can be overloaded.

You can make an implicit operator Observable<T>(T value) , however, which will allow you to implicitly convert from T to Observable<T> .

Saying, I would not recommend this. I would make the assignment explicit, since this would require the creation of a new Observable<T> each time it is called, which will cause problems with event handlers, since each assignment will create a new instance of the object.

+4
source

Add another implicit conversion in the following lines:

 public static implicit operator Observable<T>(T value) { return new Observable<T>{Value = value}; } 
+3
source

All Articles