General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 7th, 2002, 03:07 PM
ckchin ckchin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 52 ckchin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
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:
...
public 
Form1()
        {
            
//
            // Required for Windows Form Designer support
            //
            
InitializeComponent();
            
this.btnOK.Enabled false;
            
// Values for testing if the data is valid
            
this.txtAddress.Tag false;
            
this.txtAge.Tag false;
            
this.txtName.Tag false;
            
this.txtOccupation.Tag false;

            
// Subscriptions to events
            
this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtB  oxEmpty_Validating);
...
...
private 
void txtBoxEmpty_Validating(object sender
                                            
System.ComponentModel.CancelEventArgs e)
        {
            
// we know the sender is a TextBox, so we cast the sender object to that 
            
TextBox tb;
            
tb = (TextBox)sender;
            
// If the text is empty we set the background color of the 
            // TextBox to red to indicate a problem. We use the tag value
            // of the control to indicate if the control contains valid
            // information.
            
if (tb.Text.Length == 0)
            {
                
tb.BackColor Color.Red;
                
tb.Tag false;
            }
            else
            {
... 
This source code is extracted TextBoxTest example in Chapter 13.

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

Reply With Quote
  #2  
Old May 8th, 2002, 03:58 AM
James Yang James Yang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Atlanta, Georgia
Posts: 284 James Yang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 45 sec
Reputation Power: 16
Send a message via ICQ to James Yang
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/

Reply With Quote
  #3  
Old May 8th, 2002, 04:01 AM
James Yang James Yang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Atlanta, Georgia
Posts: 284 James Yang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 45 sec
Reputation Power: 16
Send a message via ICQ to James Yang
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.

Reply With Quote
  #4  
Old May 8th, 2002, 07:54 AM
ckchin ckchin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 52 ckchin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
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.

Reply With Quote
  #5  
Old May 8th, 2002, 08:01 AM
James Yang James Yang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Atlanta, Georgia
Posts: 284 James Yang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 45 sec
Reputation Power: 16
Send a message via ICQ to James Yang
bring it on ckchin!

James.NET is always at ur service(only at devarticles)

lol

Ill try my best to answer ur questions.

Reply With Quote
  #6  
Old May 8th, 2002, 08:12 AM
ckchin ckchin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 52 ckchin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Thanks in davance.

Quote:
we know the sender is a TextBox, so we cast the sender object to that


casting question: could you explain this sentence further?
PHP Code:
private void txtBoxEmpty_Validating(object sender
                                            
System.ComponentModel.CancelEventArgs e


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.

Reply With Quote
  #7  
Old May 8th, 2002, 08:33 AM
James Yang James Yang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Atlanta, Georgia
Posts: 284 James Yang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 45 sec
Reputation Power: 16
Send a message via ICQ to James Yang
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,,

Reply With Quote
  #8  
Old May 8th, 2002, 08:41 AM
James Yang James Yang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Atlanta, Georgia
Posts: 284 James Yang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 45 sec
Reputation Power: 16
Send a message via ICQ to James Yang
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 08:46 AM.

Reply With Quote
  #9  
Old May 8th, 2002, 09:00 AM
James Yang James Yang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Atlanta, Georgia
Posts: 284 James Yang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 45 sec
Reputation Power: 16
Send a message via ICQ to James Yang
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

Reply With Quote
  #10  
Old May 8th, 2002, 09:04 AM
James Yang James Yang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Atlanta, Georgia
Posts: 284 James Yang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 45 sec
Reputation Power: 16
Send a message via ICQ to James Yang
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

Reply With Quote
  #11  
Old May 8th, 2002, 12:31 PM
ckchin ckchin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 52 ckchin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
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 12:35 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > C# - subscribe to event and casting


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap