Solved

How to subscribe to an event through a delegate

Posted on 2013-11-22
6
321 Views
Last Modified: 2013-11-25
I'd like to assign an event to a delegate instead of assigning the event to the method directly.
The code example is below:

class FindPlace
{
    private delegate void QueryExecuteCompleted(object sender, QueryEventArgs args);

    private void CountyComboBox_SelectionChanged(
        object sender,
        SelectionChangedEventArgs selectionEvArgs)
    {        
        QueryTask queryTask;       
        string layerName;        
        QueryExecuteCompleted CountySelectionChanged_ExComp;        

        if (StateType == StateTypes.AbstractState)
        {        
            layerName = "TX Abstracts";
            CountySelectionChanged_ExComp =  qryCplt.AbstractQueryTask_ExecuteCompleted;          
        }
        else if (StateType == StateTypes.NonPlssState)
        {        
            layerName = "Districts"; 
            CountySelectionChanged_ExComp = qryCplt.DistrictQueryTask_ExecuteCompleted;            
        }
        else
        {     
            layerName = "PLSS Townships";
            CountySelectionChanged_ExComp = qryCplt.TownshipQueryTask_ExecuteCompleted;
        }

        queryTask = new QueryTask(layerName);           
        queryTask.ExecuteCompleted += CountySelectionChanged_ExComp;
        queryTask.ExecuteAsync(query); 
    }
}

Open in new window


That causes this error:
Cannot implicitly convert type FindPlace.QueryExecuteCompleted to System.EventHandler<QueryEventArgs>

The error occurs on this line:
queryTask.ExecuteCompleted += CountySelectionChanged_ExComp;

I can get the code to work if I do this:
//. . .
if (StateType == StateTypes.AbstractState)
{        
    layerName = "TX Abstracts";
    queryTask = new QueryTask(layerName);        
    queryTask.ExecuteCompleted = qryCplt.AbstractQueryTask_ExecuteCompleted;  }
//. . .

Open in new window


But I don't want to do that.
I'd rather prefer to have the method subscribe to the event indirectly through a delegate.
0
Comment
Question by:XTO
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39671387
I don't see where the event is defined...
0
 
LVL 2

Accepted Solution

by:
Pavel_Kudishin earned 200 total points
ID: 39672777
Please replace
queryTask.ExecuteCompleted += CountySelectionChanged_ExComp;

Open in new window

with
queryTask.ExecuteCompleted += 
new EventHandler<QueryEventArgs>(CountySelectionChanged_ExComp);

Open in new window

0
 
LVL 27

Assisted Solution

by:MikeToole
MikeToole earned 200 total points
ID: 39672841
I'm not entirely certain, but here's my thoughts...
When assigned individually, it's the method that's getting added to the delegate.
However, the  statement ...
queryTask.ExecuteCompleted += CountySelectionChanged_ExComp;
... is trying to add a Delegate where a method is expected - which will work, I think, if they're the same delegate type, but not if they're different, even when they have the same signature.

Try explicitly extracting the first member of the invocation list from your local delegate:
queryTask.ExecuteCompleted += CountySelectionChanged_ExComp.GetInvocationList()[0];

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 39673161
If you need to pass the fact that the query is completed to the caller, then you still need a public event, like this:

public event EventHandler<QueryEventArg>;

Open in new window

0
 

Author Comment

by:XTO
ID: 39673330
Thanks all.

re: Pavel,
I will try the
+= new EventHandler<QueryEventArgs>(CountySelectionChanged_ExComp);
when I get back to work tomorrow.
Hopefully the QueryTask object event handler, ExecuteCompleted, will be able to recognize the delegate as pointing to the method to execute such that the QueryTask object's ExecuteAsync method will trigger it.

re:
I will try the
+= CountySelectionChanged_ExComp.GetInvocationList()[0];
tomorrow also.
I had tried the GetInvocationList() without the array element, [0]. I have not tried it with pointing to the first method in the list as you have suggested.
I had also tried,
+= CountySelectionChanged_ExComp.Method
and
+= CountySelectionChanged_ExComp.Target
but both of those of those failed.

re: TheLearnedOne,
The QueryTask class is from a third party API. I'll see if I can access its documentation through the Visual Studio Object Explorer to get some more info on their ExecuteHandler event.
I have made certain, though, that my delegate, QueryExecuteCompleted, does have the same signature as the one that it is expecting by using the same signature as the methods that do correctly work with it.

Thanks all again.
I'll have more info tomorrow.
0
 

Author Comment

by:XTO
ID: 39675061
re, Pavel and Mike,

Both your suggestions worked brilliantly.

But first, I had to change this,
CountySelectionChanged_ExComp = qryCplt.TownshipQueryTask_ExecuteCompleted;

Open in new window

into this,
CountySelectionChanged_ExComp = new QueryExecuteCompleted(qryCplt.TownshipQueryTask_ExecuteCompleted);

Open in new window

So for instance, the else clause will look like this:
else
{     
       layerName = "PLSS Townships";
       CountySelectionChanged_ExComp = new QueryExecuteCompleted(qryCplt.TownshipQueryTask_ExecuteCompleted);
}

Open in new window

I was worried I might have to resort to some convoluted System.Reflection type code.

-------------------

re: TheLearnedOne,

Here is the info I was able to find on the ExecuteCompleted event.

This is from the metadata,
// Summary:
//     Occurs when the query completes.
[ScriptableMember]
public event EventHandler<QueryEventArgs> ExecuteCompleted;

Open in new window


And this is from Visual Studio's Object Browser,
public event System.EventHandler<QueryEventArgs> ExecuteCompleted
    Member of ESRI.ArcGIS.Client.Tasks.QueryTask

Open in new window


Summary:
Occurs when the query completes.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now