Solved

How to subscribe to an event through a delegate

Posted on 2013-11-22
6
322 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

9 Experts available now in Live!

Get 1:1 Help Now