Reputation: 5480
I don't understand why I am getting an error of:
'System.Collections.Generic.List Notify.MainPage.webClient_OpenReadCompleted(object, System.Net.OpenReadCompletedEventArgs)' has the wrong return type
Code:
webClient.OpenReadCompleted += webClient_OpenReadCompleted;
And:
private List<SightingType> webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
var sightingT = new List<SightingType>();
try
{
ser = new DataContractJsonSerializer(typeof(ObservableCollection<SightingType>));
ObservableCollection<SightingType> sightingTypes = ser.ReadObject(e.Result) as ObservableCollection<SightingType>;
foreach (var sightingType in sightingTypes)
{
sightingT.Add(sightingType);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return sightingT;
}
Does anyone know where I am going wrong?
Upvotes: 2
Views: 12388
Reputation: 21
Event handlers must return void. In your code, change:
private List webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
to
private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
That ought to do the trick.
Upvotes: 0
Reputation: 9863
This webClient.OpenReadCompleted += webClient_OpenReadCompleted;
is just registering an event. It doesn't actually run that line of code when the event exicutes.
In other words
this
webClient.OpenReadCompleted += webClient_OpenReadCompleted;
is not equal to this
var item = yourMethodCall();
If you want to retrieve the info from your completed event then you need to create a property or a global variable and assign it when the completed is finished
So instead maybe try
public List<SightingType> sightingT = new List<SightingType>();
private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
try
{
ser = new DataContractJsonSerializer(typeof(ObservableCollection<SightingType>));
ObservableCollection<SightingType> sightingTypes = ser.ReadObject(e.Result) as ObservableCollection<SightingType>;
foreach (var sightingType in sightingTypes)
{
sightingT.Add(sightingType);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Upvotes: 3
Reputation: 15247
The OpenReadCompleted
event is an event of the delegate type OpenReadCompletedEventHandler
which, according to its documentation, returns a void:
public delegate void OpenReadCompletedEventHandler(
Object sender,
OpenReadCompletedEventArgs e
)
So you can't use a function that returns something else.
Upvotes: 1
Reputation: 245479
WebClient.OpenReadCompleted
is an event. Event handlers are delegates that must define a void
return type. Your handler defines a return type of List<SightingType>
. Therefore, your handler has the wrong return type.
Upvotes: 2
Reputation: 3082
The return type of an event handler should be void:
private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
There's nothing at the other end to receive the return data.
Upvotes: 12