|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
C# - subscribe to event and casting
Hi,
I have several question on C#, and 2 of them are as follows (I use the Beginning C# by Wrox) PHP Code:
My question: What is "Subscriptions to events"? Why we want to do so? What is casting? as in "we know the sender is a TextBox, so we cast the sender object to tha". Do you have any tutorial talking about this. I think Java language has this things as well because it is OOP concept. Thanks in advance for your help.
__________________
Regrads, ckchin |
|
#2
|
|||
|
|||
|
hey!
answer to ur first question Event is raised whenever the specified condition is met. Right ?? now when event is raised, there are behind scene actions which for a lot of time can be ignored. What the event does is that notfies its subscribers through delgates. Which are like container for methods. in your code this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtB oxEmpty_Validating); method txtBoxEmpty_Validating is being subscribed to event Validating through CancelEventHandler delgate. This means when ever the event Validating is raised, it notifies the method then it is ran. This is very brief and simple description for detailed description, I really recommend Wrox's Professional ASP.NET Server Controls book. Which has a whole chapter devoted to advanced Event handling. now to ur 2nd Q casting is really easy. Casting just means changing between types (classes). "we know the sender is a TextBox, so we cast the sender object to that"This sentence just means because we know that the class that raised the event (which is sent to the handler as object class called sender) is textbox object, we can jsut cast it to textbox with out raising any error caused when you try to cast an object to an incompatible object such as when you try to convert sqlconnection object to sqlcommand object. In this case it's a Textbox object casted to object class(named sender), it can be converted (casted) back to textbox object easily. hope this helps
__________________
Regards, James Yang .NET Developer / Network Engineer MCSE, MCDBA, MCSA, CCNA http://www.yellowpin.com/ http://www.opentechsupport.com/ |
|
#3
|
|||
|
|||
|
Adding to that response,
your code very much look like a event handling code for an asp.net server control, in which case it is real good to know how event works in .NET EXACTLY, inside out, as event in server controls are very important... and for this I STRONGLY recommend the book Professional asp.net server controls by wrox. |
|
#4
|
|||
|
|||
|
Thanks for your reply. I actually have many questions on C# and will sort them out ask them from you through the forum. I hope you can help me out. Thanks again.
|
|
#5
|
|||
|
|||
|
bring it on ckchin!
James.NET is always at ur service(only at devarticles) ![]() lol Ill try my best to answer ur questions. |
|
#6
|
|||
|
|||
|
Thanks in davance.
Quote:
casting question: could you explain this sentence further? PHP Code:
What is object sender? Why it not abc xyz, or object xyz, or abc sender? What is System.ComponentModel.CancelEventArgs e? I read the help from MSDN, and do not understand it quite well. Are these terms fixed by the C# programmer? Yes, this might silly question , but I still do not understand them. |
|
#7
|
|||
|
|||
|
ok
when you just put object sender; you are creating an instance of object class called 'object' and that instance is called sender object class is what EVERY class in .NET inherits from, and by accepting object class (or object type to make it easier to understand) a parameter, ANY object can be passed as parameter, because any object can bed CASTED(changed) to an instance of object class. now this si where that sentence come in. When a object is casted FROM instance of object class to other richer class (which is inheritted from object class), it is only possible to do this if the object has past history of being that class. "we know the sender is a TextBox, so we cast the sender object to that " so in the above sentence. an instance of textbox object is passed through a 'delegate' (which ill explain soon) as an instance of object class as it only accept parameter as object class NOT textbox object. This is because the delegate is also accepting parameters that are not originated form the textbox, so by accepting object class it is open to any object. Now.. the sentence is saying that the object that raised the event is Textbox. And we know that for a fact as the person who said it programmed the app. so it is VERY safe to cast it in to Textbox as we know that, TAHT instance of object has history of being textbox object. This when you think about it, its very logical say class 'classa' has two methods method methodb and methodc and object has no methods( this is not true though but for this purpoose i decided that object class has no methods) now by having classa ClassName; we are reserving memory space for the methods and properties. And becuz we have two methods, .NEt will reserve space for two. now when you cast that to object class which is automatic when event is raised (object)ClassName you loose ability to access methodb and methodc. But not loose them forever. by casting back to classa, classa(ClassName) you can access those again. Now if you try to cast it back to classb which might have 5 methods or 1 methods or even two methods that are not inheritted from classa (ill expand this) it will cause error so to do this casting we must know that ClassName has been classa type once upon a time.... ok that's casting back from object class to any other class now about casting from a class, up the hierarchy (object class and some of common base class).. you can cast to any object that you have derived from. and cuz EVERyting derive form object class everything can cast to that.... phew that's 1st question answered 2nd one coming in a min,, |
|
#8
|
|||
|
|||
|
object is a class name in that code
but object is also an instance of a class. so you can have object object. which is an instance of object class now for that code you can have private void txtBoxEmpty_Validating(object sdf23fwefr, System.ComponentModel.CancelEventArgs df26835f82) as long as you accept a object instance and CancelEventArgs class no more no less Last edited by James Yang : May 8th, 2002 at 07:46 AM. |
|
#9
|
|||
|
|||
|
k last Q
What is System.ComponentModel.CancelEventArgs e? CancelEventArgs is a (user? pre? I dunno) defined class that inherits from EventArgs class. Meanign ti add funcionality of EventArgs + its own. What i noticed in your previos code was this CancelEventHandler now this is a delegate. Delegate has alot of diff uses but in event it is used to define the signature (info on what parameters a method is going to accept) for methods that handle a event. for eg. Click event has EventHandler delegate and this delegate specifies that eventhandler(method) for this event MUST accept instance fo object and EventArgs class. EventArgs class give most basic info on the event and usually its no use to us what so ever. Now why do we have it ? cuz we can define our own events, delegates and event handlers and ingerit this eventargs class and add more property to make it more useful tghe code u posted has this line this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtB oxEmpty_Validating); here Validating is the event it is raised when a condition is met. prolly when textbox goes thru a process of validation. then when event is raised. Delegate assigned to this event, CancelEventHander runs thru the its subscribed methods(event handler) now its not shown but the CancelEventHandler delgate is defined b4 this code prolly public delegate void CancelEventHandler(object sender, CancelEventArgs e) this defines the signature. This restricts what method can be added to the list of methods to run when event is raised so for txtBoxEmpty_Validating to handle the event or become subscribed to the delegate handlingthe vent, it MUST have the signature defined by the delgate (object sender, CancelEventArgs e). so it does got it.? this topic is very complicated. I think it is more complicated cuz it involves delegates which are very very useful but hard to explain. I think it will be easier for tyou to understand this topic once you become mroe familiar with OOP. like inheretance and etc |
|
#10
|
|||
|
|||
|
oh i forgot about what EventArgs are
well basically it can contain info for the event for Click event you can have more advanced click event that uses your userdefined delegate that might take AdvancedEventArgs class which has properties for the location x,y... how usefull would that be... your method can then accep the parameter AdvancedEventArgs e and access the property e.Xand e.Y the best book on this is Pro asp.net server controls.. if you need furthur help or help with understading what i wrote..lol give me a yell |
|
#11
|
|||
|
|||
|
Thanks. Now I go to amazon to have a look on the bokk recommended and any best book on C#. I am now only using one Beginning C#, which make me full of silly questions as asked by me. I like C# but I hate OO or may be the book used by me is not a good one
talking on C# OR I AM NOT SUITABLE TO LEARN C# AND OOP. Any good book .... ![]() Last edited by ckchin : May 8th, 2002 at 11:35 AM. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > C# - subscribe to event and casting |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|