I often get some preliminary warnings regarding "unassigned objects created using the" new "expression." The following code snippet should demonstrate the situation:
I use a helper class ( Observer.cs ) that controls some properties from another class ( MonitoredClass.cs ). When a property changes, the observer class writes the changed value to another data class ( DataClass.cs ).
simplified code:
MonitoredClass.cs:
public class MonitoredClass : INotifyPropertyChanged {
DataClass.cs:
public class DataClass { public string LastProperty1Value { get; set; } }
Observer.cs:
public class Observer { private MonitoredClass _monitoredClass; private DataClass _dataClass; public Observer(MonitoredClass monitoredClass, DataClass dataClass) { _monitoredClass = monitoredClass; _dataClass = dataClass; _monitoredClass.PropertyChanged+=MonitoredClassPropertyChanged; } private void MonitoredClassPropertyChanged(..) { _dataClass.LastProperty1Value = _monitoredClass.Property1; } }
So far so good.
If now I use my Observer class as follows:
... new Observer(monitoredClassInstance, dataClassInstance); ...
than I get a warning of a stepwise "possible unassigned object created by a" new "expression."
My question now is if there is a better solution / template for developing this observer. From the coarse, I can single out a new instance of the observer in a private field. But I have a field that is never used. Or I can set the properties monitoredClassInstance and dataClassInstance with properties instead of passing them in the constructor. But this only prevents the warning, but does not really change the architecture.
Thank you in advance for your advice, opinions, samples, etc.
rhe1980
source share