Tuesday, December 1, 2009

Compile error 'Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection'.

While trying to call a WCF service and assigning a resultant array to a local variable, you may encounter an compile error 'Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection'.

How to resolve this error.
below might be code you might be trying to compile

void proxy_GetUOSListCompleted(object sender, SilverScreens.SilverServiceProxy.GetUOSListCompletedEventArgs e)
{
//Here result returns a List object
SilverServiceProxy.UOS[] ResultUOS = e.Result;
UOSList.ItemsSource = ResultUOS;
}

Following are the steps to resolve it.

Right click on Service Reference in your Webapplication. In the context menu select "Configure Service Reference...", choose in the combobox of collection type: System.Array.

The other work around is to change the code to use ObservableCollection collection, since ObservableCollection also implements suitable property changed notification mechanism such as the INotifyPropertyChanged interface

void proxy_GetUOSListCompleted(object sender, SilverScreens.SilverServiceProxy.GetUOSListCompletedEventArgs e)
{
//Here result returns a List object
List&; = (List&;).Result.ToList();
UOSList.ItemsSource = ResultUOS;
}

One other observation was that while compiling the above code resulted in the compile time error that ToList() operator could not be associated. To resolve this issue, add the system.linq namespace statement.

1 comment:

Anonymous said...

saved my life..thanks