Solved

How to subscribe to an event through a delegate

Posted on 2013-11-22
6
320 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wpf develop 5 32
Image(2) 3 26
Set form below another form 3 26
cs.Designer Issue(2) 2 21
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

17 Experts available now in Live!

Get 1:1 Help Now