Delegates, Events and Generics in C - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Delegates, Events and Generics in C

Description:

... unpreventable problems (open non-existent file, run out ... subscribers use event handler ( =, -=) event keywords stops direct invocation. Creating an Event ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 11
Provided by: Robert9
Category:

less

Transcript and Presenter's Notes

Title: Delegates, Events and Generics in C


1
Delegates, Events and Generics in C
  • CS2335
  • Spring 2007

2
Exceptions
  • Dont use to find handle bugs fix the bug!!
  • Dont use to detect user mistakes write
    validation code and use return codes
  • Use for predictable but unpreventable problems
    (open non-existent file, run out of memory, etc.)

3
A lot like Java!!!
  • try / catch block
  • We can have simple catch that gets everything
  • try catch
  • Or specify the exception
  • try catch (System.ArithmeticException)
  • finally ensures completion
  • try catch finally
  • cannot have break, return, goto or continue in a
    finally block.

4
Throwing exceptions
  • throw anException instance
  • throw new System.Exception()
  • execution immediately halted and CLR starts
    unwinding looking for handler.
  • if no handler, exception is passed to the VM
    which shows the user an exception dialog.

5
Custom Exceptions
  • Derive from System.ApplicationException class
  • constructor takes a string and passes to base
    class
  • public MyException(string msg) base (msg)

6
Built-in exceptions
  • Never throw System.Exception or
    System.ApplicationException
  • Dont just blindly catch all exceptions, know
    what you are looking for, Dont try to handle
    exceptions that you cannot.
  • System.OutOfMemoryException
  • System.NullReferenceException
  • System.InvalidCastException
  • System.IndexOutOfRangeException

7
Delegates
  • Loose coupling mechanism Buttons
  • Delegates are first-class objects
  • Syntactic sugar for function pointers
  • typedef int (CMP_FUNC_PTR)(void , void )
  • public delegate int CMP_FUNC (object x, object
    y)
  • Like an interface with only one method
  • Add delegate , Remove delegate -

8
Events
  • exist as class members just like attributes or
    properties
  • uses the publish/subscribe model
  • publishing class raises event (invokes delegates)
  • subscribing class is notified of event
  • publishing uses delegates
  • subscribers use event handler (, -)
  • event keywords stops direct invocation

9
Creating an Event
  • Create a class to handle the event data (or
    arguments) Subclass EventArgs
  • In the class raising the event, declare the
    delegate method signature everyone must
    implement.
  • In the class raising the event declare the
    delegate with the event keyword
  • In the class listening for the event, implement
    the delegate method and register it with the
    class raising the event.

10
Event Conventions
  • Signature is always (object sender, EventArgs
    args)
  • Name always begins with On
  • If you dont need args, you still declare them,
    but pass EventArgs.Empty
Write a Comment
User Comments (0)
About PowerShow.com