I am working on creating a base view model class. ViewModelBase is an abstract class and I want to define the properties that I want all my other derived view models to implement.
One of the properties is an ObservableCollection:
public abstract ObservableCollection<???> Items { get; set; }
The classes that derive from this base class will have different types of Items defined (ObservableCollection<Person>
, ObservableCollection<Car>
).
If I set the ObservableCollection type to object
in ViewModelBase, it would require me to do a lot of different casting in the derived classes to get it to work.
Is this the right approach?
I'm not quite sure why you would want to make it so generic, but if you did, I'd recommend that you make the abstract base class generic as well:
public abstract class ViewModelBase<T>
{
public abstract ObservableCollection<T> Items { get; set; }
}
I hope you also make sure that your ViewModelBase implements INotifyPropertyChanged.
public abstract ObservableCollection<TYPE> Items { get; set; }
You can define the TYPE in many ways, including when using/inheriting from the base class, or Interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With