none
Deleted RRS feed

All replies

  • SelectionHandler is passed to each call to AddAutomationEventHandler. As documented here the handler is called for each automation event. The sample implementation doesn't do anything in the handler but has a TODO comment letting you know you can put your event handling code there. At a minimum consider putting a little code in there to look at the event args and then debug your code and see it being called.

    As for the dummy variable, it doesn't serve any purpose in current versions of C#. In the original version of C# (which this code probably targeted) you had to wrap delegates because the compiler couldn't do type inferencing like it can now. You can remove the dummy variable.


    Michael Taylor http://www.michaeltaylorp3.net


    • Edited by CoolDadTxModerator Friday, December 6, 2019 2:52 PM Note about type inferencing
    Friday, December 6, 2019 2:51 PM
    Moderator
  • Deleted
    Friday, December 6, 2019 2:54 PM
  • Refer to the updated comment I put in.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, December 6, 2019 3:01 PM
    Moderator
  • Deleted
    Friday, December 6, 2019 3:05 PM
  • Type inferencing is the ability of the compiler to infer a type given its usage. For example

    //10 is an int literal therefore the right expression must be an int
    int x = 10;
    
    //20 is an int literal therefore the right expression must be an int and therefore y would need to be an int to be compatible - type inferencing
    var y = 20;

    Type inferencing allows the compiler to determine the type of something without you having to be explicit. This generally produces more readable code and makes it easier to maintain, but not always.

    In the early days of the language type inferencing was the same as other languages like C++ so the compiler did very little.

    //We know this is a method that takes an Object and AutomationEventArgs and returns void
    void SelectionHandler ( object source, AutomationEventArgs e )
    { }
    
    //This is a type that represents a method that takes an Object and AutomationEventArgs and returns void
    void AutomationEventDelegate ( object x, AutomationEventArgs y );
    
    //This method requires an argument that is of that delegate type
    void AddAutomationEventHandler ( AutomationEventDelegate delegate ) 
    {}
    
    //Original C# did not do type inferencing on methods and therefore it could not automatically infer that a method signature matches a delegate, you had to do it yourself.
    var handler = new AutomationEventDelegate(SelectionHandler);
    
    AddAutomationEventHandler(handler);
    
    //C# extended support for type inferencing such that it can infer that a method matches the signature of a delegate without the need for you being explicit - type inferencing
    AddAutomationEventHandler(SelectionHandler);


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, December 6, 2019 3:12 PM
    Moderator