I am having an issue whereby I cannot use an objects method because of this error:
Does not contain a definition…and no extension method…
It is very related to this question. I am doing what is given as the answer in that question, but I am still getting this error.
namespace MyProject.ViewModel
{
public class NetworkHealthViewModel : ViewModelBase
{
private IDataService _dataService;
public ObservableCollection<NetworkBandwidthModel> NbicNetworkBandwidth
public ObservableCollection<NetworkPortalStatusModel> NbicNetworkPortalStatus
public NetworkHealthViewModel()
{
_dataService = new DataServiceNetworkHealth();
NbicNetworkBandwidth = new ObservableCollection<NetworkBandwidthModel>();
NbicNetworkPortalStatus = new ObservableCollection<NetworkPortalStatusModel>();
_dataService.LoadChartItems(NetworkBandwidthLoaded, NetworkBandwidthLoadedFailed);
_dataService.LoadPortalStatus(NetworkPortalStatusLoaded, NetworkPortalStatusLoadedFailed);
}
The error lies at LoadPortalStatus(). LoadChartItems() is fine. NetworkBandwidthLoaded and NetworkPortalStatusLoaded are delegates.
NetworkPortalStatusLoaded is laid out pretty much the same as NetworkBandwidthLoaded:
private void NetworkPortalStatusLoaded(IEnumerable<ChartModel> portalStatItems)
{
NbicNetworkPortalStatus.Clear();
var networkPortalItems = from item in portalStatItems
where ((NetworkPortalStatusModel)item).Unit == "Portal"
select item;
foreach (var item in networkPortalItems)
{
NbicNetworkPortalStatus.Add((NetworkPortalStatusModel)item);
}
Message = "Network Portal details loaded";
}
My DataServiceNetworkHealth class is defined as:
namespace MyProject.DataServices
{
public class DataServiceNetworkHealth : IDataService
{
private Action<IEnumerable<ChartModel>> _delagateSuccess;
private Action<Exception> _delagateFail;
private String _portalHtmlResponse;
public void LoadChartItems(Action<IEnumerable<ChartModel>> success, Action<Exception> fail)
{
....
}
public void LoadPortalStatus(Action<IEnumerable<ChartModel>> success, Action<Exception> fail)
{
....
}
}
}
Method LoadChartItems() is defined in the interface IDataService, but LoadPortalStatus is not. Perhaps that’s where the problem lies. There are other DataServiceX classes that do not use LoadPortalStatus.
I know this is a long post, but I thought better to give all info up front. 🙂 Thanks for any help!
Your reference to
_dataServiceis of the typeIDataService, which means that you can only access members defined by this interface (or extension methods for said interfae). You mention thatLoadPortalStatusisn’t part of that interface and thus you cannot access it through the_dataServicereference.Updated based on comment: Let’s look at another example. If you define a
List<int>reference like this:you can access all the members of
List<T>()and all the members ofObject. However, if you change the reference tothe only accessible members through
iareGetEnumeratorwhich is defined onIEnumerable<T>and the members inherited fromObject.If you include the
System.Linqnamespace, you also get access to the numerous extension methods defined forIEnumerable<T>.The static type of
idetermines which members are available through that reference.