Cannot resolve symbol T
-
16-09-2019 - |
Question
I have a problem with ObservableCollection Class. I cant resolve this.
using System.Collections.ObjectModel;
#region ViewModelProperty: CustomerList
private ObservableCollection<T> _customerList = new ObservableCollection<T>();
public ObservableCollection<T> CustomerList
{
get
{
return _customerList;
}
set
{
_customerList = value;
OnPropertyChanged("CustomerList");
}
}
#endregion
My class with the ObservableCollection inherits ViewModelBase:
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
any idea, where is the problem?
Solution
T
is just a placeholder. You need to supply an actual type somewhere for T
.
E.g. if you have List<T>
, you could make a List<int>
or List<string>
(T
is any other type which fits the given constraints). I.e. T
is int
in the first case and string
in the second.
OTHER TIPS
I think that it will become clear to you when you read up a bit on Generics
You could do it like this:
ObservableCollection<Customer> customerList = new ObservableCollection<Customer>()
Then, you have a typed collection which will be able to store instances of the Customer class (and also instances of subclasses that inherit from Customer). So, if you want to have a collection where you want to be able to add instances of multiple types, you could create a base-class (or interface), and inherit from that base-class or implement this interface, and create an ObservableCollection instance for ex.
As previously posted, T is a placeholder. You probably want something like:
private ObservableCollection<T> _customerList = new ObservableCollection < ClassYouWantObserved > ();