(Notes from HFC#)
Steps in Event execution
1)We need an event for event arguments
eg-> public class BallEventArgs : EventArgs
=> EventArgs is an empty class.It has no public members.Its sole purpose is to allow
your event arguments to be passed to the event handlers that use it .
2)Need to define the event in the class that'll raise it .
eg-> public event EventHandler BallInPlay
=>after EVENT keyword comes EVENTHANDLER.It is needed to tell object subscribing to event that
their event handlers need to take 2 parameters,an object named SENDER and an EVENTARGS reference names e,
and have no return value(void).
3)The subscribing classes need EventHandler methods
eg-> void ball_BallInPlay(object sender,EventArgs e )
=>Standard Naming Convention for EventHandlers -> NameOfObjectReference,followed by UNDERSCORE ,followed by
NameOfEvent.
4)Each individual object subscribes to the event
eg-> ball.BallInPlay += new EventHandler(ball_BallInPlay)
=>The += EVENT tells C# subscribe an event handler to an event.
5)A Ball Object raises its event to notify subscribers that it's in play
ex- if (BallInPlay !=null) BallInPlay(this,e)
Note: If you raise an event with no handlers ,it'll throw an exception.
Wednesday, May 7, 2008
Virtual / Overridable methods in .NET
Virtual / Overridable methods are those which may or may not implemented in the derived class,but they must have some implementation in the base class.
When a derived class method overrides its baseclass method the signature should be same as in the base class, this includes the modifier, parameters and the return type of the method, any modifications in the derived class will lead to a compilation error.
If method is declared virtual in the base class then it is not mandatory to override that function in the derived class. In fact that method need not be a part of the derived class at all. However if the method with the same signature is present in the derived class then it needs clarify whether it is overriding the base class member or not.
a)If the method in the derived class is ment to override the base class member then it needs to have the keyword 'override'
b)If the method in the derived class is not ment to override the base class member then it needs to have the keyword new [Shadows in vb.net] , this is called 'Hiding the Base Class Member'
c)If the method in the derived class has the same name as in the base class but if it has a different signature then it is not required to state either override or new since this method neither overrides not hides the base class method.
These options are not available for an Abstract method, a method declared Abstract in the Base Class must ob overridden in the derived class, it cannot hide the base class member using the 'new' keyword.
When a derived class method overrides its baseclass method the signature should be same as in the base class, this includes the modifier, parameters and the return type of the method, any modifications in the derived class will lead to a compilation error.
If method is declared virtual in the base class then it is not mandatory to override that function in the derived class. In fact that method need not be a part of the derived class at all. However if the method with the same signature is present in the derived class then it needs clarify whether it is overriding the base class member or not.
a)If the method in the derived class is ment to override the base class member then it needs to have the keyword 'override'
b)If the method in the derived class is not ment to override the base class member then it needs to have the keyword new [Shadows in vb.net] , this is called 'Hiding the Base Class Member'
c)If the method in the derived class has the same name as in the base class but if it has a different signature then it is not required to state either override or new since this method neither overrides not hides the base class method.
These options are not available for an Abstract method, a method declared Abstract in the Base Class must ob overridden in the derived class, it cannot hide the base class member using the 'new' keyword.
Thursday, April 17, 2008
Intro to Design Patterns
Design Principle ->Identify the aspects of application that vary and seperate them from what stays the same i.e.
=>take the parts that vary and encapsulate them,so that later you can alter or extend those parts which v ary without affecting who don't.
=>take the parts that vary and encapsulate them,so that later you can alter or extend those parts which v ary without affecting who don't.
Subscribe to:
Comments (Atom)