Chapter 13' Graphical User Interface Concepts: Part 1 Delegates - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Chapter 13' Graphical User Interface Concepts: Part 1 Delegates

Description:

methods is specified when the delegate class is defined. ... public static int DoSomeMath(MathOp fn, int x, int y) return fn(x, y); //Invoke the delegate ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 15
Provided by: beiz
Category:

less

Transcript and Presenter's Notes

Title: Chapter 13' Graphical User Interface Concepts: Part 1 Delegates


1
Chapter 13. Graphical User Interface Concepts
Part 1Delegates
2
Delegates
  • Simplest description type-safe function pointers
  • Declaring a delegate defines a class that derives
    from System.MulticastDelegate, which derives from
    System.Delegate

3
Delegates
  • Sometimes useful to pass methods as arguments to
    other methods
  • Delegates are sets of references to methods
  • Delegate objects can be passed to methods
    methods can then invoke the methods the delegate
    objects refer to
  • Delegates that contain one method are known as
    singlecast delegates and are created or derived
    from class Delegate
  • Delegates that contain multiple methods are known
    as multicast delegates and are created or derived
    from class MulticastDelegate

4
Delegates
  • Delegates must be declared before use
  • A delegate declaration specifies the parameters
    and return type of the methods the delegate can
    refer to
  • Methods who can be referred to by a delegate,
    must have the same signature as the delegate
  • Delegate instances can then be created to refer
    to a method(s)
  • Once a delegate instance is created, the method
    it refers to can be invoked

5
Delegates
  • Delegates maintain a list of methods to be called
    when the delegate is invoked.
  • To insure type safety, the signature (return type
    and arguments) of acceptable callback
  • methods is specified when the delegate class is
    defined.
  • We instantiate a delegate by "wrapping" the
    callback method.
  • We can add methods to the callback chain or
    remove methods from the chain.

6
Using Delegate
  • First must declare one.
  • The delegates declaration specifies a method
    header (parameters and return value).
  • Methods whose references will be contained within
    a delegate object must have the same method
    header as that defined in the delegate
    declaration.
  • Second create methods that have this signature.
  • The third step is to create a delegate instance
    that contains a reference to that method.
  • Finally we can invoke the method reference that
    it contains. We show this process in our next
    example.

7
  • using System
  • delegate int MathOp(int x, int y) //Declare
    delegate type
  • class Class1
  • static void Main(string args)
  • MathOp f1 new MathOp(Class1.Add)
    //Instantiate
  • MathOp f2 new MathOp(Class1.Mult)//delegates
  • Console.WriteLine(DoSomeMath(f1, 2, 3))
  • Console.WriteLine(DoSomeMath(f2, 2, 3))
  • public static int DoSomeMath(MathOp fn, int x,
    int y)
  • return fn(x, y) //Invoke the delegate
  • public static int Add(int a, int b)
  • return a b

8
(No Transcript)
9
  • class DemoClass
  • private string name
  • private int number
  • public DemoClass(string str, int x)
  • name str
  • number x
  • public void ShowMyName()
  • Console.WriteLine("My name is " this.name)
  • public void ShowMyNumber()
  • Console.WriteLine("My number is "
    this.number)

10
  • delegate void DemoOp() //Declare delegate type
  • static void Main(string args)
  • //Create some objects
  • DemoClass dc1 new DemoClass("Demo 1", 1)
  • DemoClass dc2 new DemoClass("Demo 2", 2)
  • //Declare an empty delegate
  • DemoOp demoDel null
  • //Append a method to list
  • demoDel new DemoOp(dc1.ShowMyNumber)
  • //Append another one
  • demoDel new DemoOp(dc2.ShowMyName)
  • //Invoke the delegate
  • demoDel()

11
(No Transcript)
12
  • class DemoClass
  • private string name
  • private int number
  • public DemoClass(string str, int x)
  • name str
  • number x
  • public void ShowMyName()
  • Console.WriteLine("My name is " this.name)
  • public void ShowMyNumber()
  • Console.WriteLine("My number is "
    this.number)

What is "this" when the method is called via
a delegate?
13
  • delegate void DemoOp() //Declare delegate type
  • static void Main(string args)
  • //Create some objects
  • DemoClass dc1 new DemoClass("Demo 1", 1)
  • DemoClass dc2 new DemoClass("Demo 2", 2)
  • //Declare an empty delegate
  • DemoOp demoDel null
  • //Append a method to list
  • demoDel new DemoOp(dc1.ShowMyNumber)
  • //Append another one
  • demoDel new DemoOp(dc2.ShowMyName)
  • //Invoke the delegate
  • demoDel()

14
Delegates Behind the Scenes
Write a Comment
User Comments (0)
About PowerShow.com