Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone explain "Source As Object, E as EventArgs" Please. 3

Status
Not open for further replies.

JGKWORK

IS-IT--Management
Joined
Apr 1, 2003
Messages
342
Location
GB

Hi,

I have began to learn ASP.NET just this week. I am coming from classic ASP (developing for about a year or so).

The book I'm reading uses the Page_Load page event alot but never explains the arguments' purpose:

Sub Page_Load(Source As Object, E as EventArgs)

Can someone please explain what the "Source As Object, E as EventArgs" arguments DO and WHY are they there?

MANY thanks.
 
In .NET events, you have signatured delegates (like function pointers in C++) that event handlers subscribe to. The object, in this case "source" is the control that raised the event, and the EventArgs, "e", contain information relevent to an event (like subclassed EventArgs containing date data for a calendar control or something).

As long as an event handler has the required signature, you can add it to the the given event's handlers collection. For example, you can have a function:

Sub MyFunction( o As Object, ea As EventArgs )
...
End Sub

and have it be processed when the Load event is raised by adding it to the handlers collection via AddHandler.
 
Errrr, looks like I have a lot to learn! I must admit I didn't understand any of that! Thanks for the help, I think I should go and look up some of this on the net.

Cheers.
 
I guess that was sort of full of buzz words.

Basically, if you have a function:

funtion( int i, string s )

then the "signature" would be that the function takes an int and a string...

anotherFunction( int i, string s )

would have the same "signature".

A delegate is basically something that defines the signature without implementing anything, for instance, a delegate can say "I require an Object and EventArgs".

An event, then, looks for functions with a specific delegate signature for processing, for instance the Load event looks for an Object and an instance of EventArgs. This can be in the form of any function that follows the "signature":

func1( Object o, EventArgs e )
func2( Object ob, EventArgs ea )
etc.


For each Event, you can keep track of a collection of such functions, and they will all be processed when the Event is raised. For example, if Page_Load and MyFunction are both part of the Load Event's collection, both functions will get processed when Load happens.

Does that help any?
 
If I could just add something that might be helpful (not contradicting, because BB's explanation is certainly correct).

Those arguments are more useful in other EventHandlers, such as those for ButtonClicks or the like, and here's why:

You can have three buttons that have the same event handler. Using the 'sender' argument, you can figure out which of the buttons were clicked.

((Button)sender).ID //for example

As for the EventArgs argument, it often times has useful information about the event, as well.

For instance, CommandEventArgs has a property called .CommandName, and another called .CommandArgument

So, you could assign the three buttons to all call on the same EventHandler. The three buttons, though, obviously have different actions that should be called. One is an Add button, another is a Delete button, and the third is a Modify button, so your button might look like this:

<asp:Button CommandName=Add id=btnAdd OnCommand="HandleCommand" Runat=Server>Add</asp:Button>

So your EventHandler might look like this:

protected void HandleCommand(object o, CommandEventArgs e)
{
switch(e.CommandName.ToString().ToLower())
{
case "add":
AddSomething();
break;
case "modify":
ModifySomething();
break;
case "delete":
DeleteSomething();
break;
}//switch
}

Where AddSomething(), ModifySomething(), & DeleteSomething() are all methods in your class that do their respective tasks.

You can even add a .CommandArgument so that you know WHAT to add, modify or delete:

<asp:Button CommandName=Add CommandArgument="Car" id=btnAdd OnCommand="HandleCommand" Runat=Server>Add</asp:Button>

so then, you can send that argument along, as well:

protected void HandleCommand(object o, CommandEventArgs e)
{
string cmdArg = e.CommandArgument.ToString().ToLower();
switch(e.CommandName.ToString().ToLower())
{
case "add":
AddSomething(cmdArg);
break;
case "modify":
ModifySomething(cmdArg);
break;
case "delete":
DeleteSomething(cmdArg);
break;
}//switch
}

So now, you're not only telling the AddSomething method to add SOMETHING, but you're telling it to add a 'CAR'.

You can see how this has the potential to greatly reduce the amount of code that must be written. It adds a great amount of flexibility so that you can reuse the same methods for lots of different things.

Quite honestly, you probably won't use those arguments very much in your fledgling days as a .NET developer (a fully functional application could be written w/o ever using them), but as your skills increase (along with the complexity of your projects), you will make more and more use of them.

Hope that clears things more than it muddies.

:-)
-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks for having a bash, I'm still stuggling (I think I'm trying to run before I've learned to walk here), one question what do the "Object and an instance of EventArgs" actually do then?

Many thanks guys.
 
Wow, those are great explanations!

To answer your next question in the simplest of terms:
"Object" indicates which object the command is coming from and "EventArgs" indicates what event is being handled.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top