Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > MS Technologies ( ASP, VB, C#, .NET )
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-02-2004, 03:58 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
Delegates - A Real Practical Example

I've been rackin my brain over delegates the last day. I have figured out in a sense HOW to use them, but I don't understand why.

Why can't we just call the class directly? I need a real world example of why we use delegates ( besides an event example )

Anyone?
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 03-03-2004, 11:38 AM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
Delegates....

Here is my two bits to your question. Delegates (as I understand them) are just a typesafe way of passing pointers to member-functions/or functions in a type-safe way. Previously (in C++ and such) we just had a memory address, and we could call it with anything, which might not be what the function wants. Also, it doesn't hold any state information about the function. Delegates make it a type-safe way to call member functions.

A real world example besides an event handler would be anywhere you have used pointers to functions. For instance, I used them in a MUD I did for CS290 to handle how certain commands are processed. (Not 'events' but probably more like it than you wanted). For instance, someone typed "GO WEST" and I passed WEST to the function that was pointed to by the map of strings to functions. In this case, the function 'GO'. However, because of the uniformity of the arguments (I didn't pass any integers or anything) there wasn't any problems making sure there were sufficient arguments or types the the function because it was all standardized.

Another example of where you would use these are for dynamic validation of data. For instance, someone passes you a phone number, and depending on where you are located, or selected country, you can customize the function that will handle that phone number to validate it.

This is what I think, if I am wrong about what delegates are, please ignore/correct my post!

Hope this helps,
Ted Morse
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 03-03-2004, 12:58 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
thanks ender. the go west example helps .. i think i just need to get stuck in one of my programs where i have no other choice but to use a delegate.

i even got delegates working between forms, basically just one form referencing a method in another form, .. but i had to pass the delegate from one form to the other.

i thought, why not just pass the form from itself to the other.

still confused, but a little less.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 03-05-2004, 10:15 AM   #4 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
Check this: http://www.c2.com/cgi/wiki?ObserverPattern

Delegates are similar to Java listeners, but they more closely resemble the SubscriberNotify or Observer pattern in purity. Where java listeners are objects forced to implement the specified interface, delegates are mere functions with a fixed signature.

I can't think of a better way to describe their value, but mostly delegates are just nice for code clarity and cleanliness. Similar implementations in other languages are usually pretty ugly in comparison and the code isn't quite as obvious in its purpose.
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 03-19-2004, 04:30 PM   #5 (permalink)
dubsonic
Registered User
 
Join Date: May 2002
Posts: 10
dubsonic is on a distinguished road
Send a message via AIM to dubsonic
hex siggy

hey sdeming, what's the deal with the hex in your signature.

just curious
__________________
dubsonic is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What do you do in the real world? Punch-M6.net Lounge 69 01-21-2008 10:46 PM
`top` doesnt display real amount of RAM? Admin Linux / BSD / OS X 1 12-18-2003 12:43 PM
C++ help (Real Time) Gorbash9k Standard C, C++ 7 03-27-2003 06:26 PM
If you had a good source list in real life ... sde Lounge 2 02-27-2003 04:57 PM


All times are GMT -8. The time now is 02:19 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle