Wrox Programmer Forums
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 11th, 2003, 04:30 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default Delegates

Dear Guys,

I have been reading about delegates and how they can be used to wrap methods so that they can be passed to other methods as parameters. An example being Events. Now, I still have not really grasped it?? Could some one please help

Thanks

Adnan Arab

Adz - Portsmouth Massive
__________________
Adz - Learning The J2EE Ways.
 
Old December 18th, 2003, 03:05 AM
Registered User
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I'm still trying to wrap my own mind around the delegate thing, but here is a more simpler example than what's in the book.

using System;

namespace Timer
{

    class Clock
    {
        delegate void Tick(int hrs, int min, int sec);

        public void RefreshTime(int hrs, int min, int sec)
        {
            Console.WriteLine("The new time is: {0}:{1}:{2}", hrs, min, sec);
        }


        [STAThread]
        static void Main(string[] args)
        {
            Clock wall = new Clock();
            Tick m = new Tick(wall.RefreshTime);

            m(12,29,59);


        }


    }
}

We have a class of Clock with a method that writes the hours, min, and seconds to the screen called RefreshTime(). Our delegate Tick also has the same param so we can now use it to call this method. In the Main, we create a Clock object. This will allow us access to the RefreshTime method. We can now create an Tick type object called "m". Now instead of writing long winded paths back, m now can take on param that matched Tick, which also match RefreshTime(). m now resolves to wall.RefreshTime(12,29,59).

Hope this helps

Ray Bohnenstiehl


 
Old December 25th, 2003, 04:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

You can think of delegates as something that you define to give a name to a method signature. If you have any idea about function pointers in c++ you can think of delegates as a datatype that can be instanciated to point to a function so that that instance can be used later on to call the function or to be passed across other functions etc just like any other variable.

Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
 
Old December 31st, 2003, 12:57 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

Nice one guys, it all clear!

Adz - The World is not enough





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with delegates mike_remember ASP.NET 1.0 and 1.1 Professional 2 November 7th, 2006 12:33 AM
Delegates RalphJr C# 2005 1 April 29th, 2006 08:06 AM
Delegates pramos.21d C# 1 April 11th, 2006 03:43 AM
Help with Delegates mike_remember ASP.NET 1.0 and 1.1 Basics 4 October 4th, 2005 07:32 AM
I still Don't get Delegates p_nut33 Pro VB.NET 2002/2003 2 April 26th, 2004 01:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.