Subby
Subby

Reputation: 5480

Wrong Return Type

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

Answers (5)

Chris
Chris

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

DotNetRussell
DotNetRussell

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

Tim
Tim

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

Justin Niessner
Justin Niessner

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

Keith Payne
Keith Payne

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

Related Questions