CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2006
    Posts
    69

    C# delegate question

    Hi,

    i've seen some code with delegate in it

    for example :

    Code:
    Figure figure = new Figure(10,20,30);
    FigureDelegate fx = new FigureDelegate(figure.InvertX); 
    FigureDelegate fy = new FigureDelegate(figure.InvertY); 
    FigureDelegate fz = new FigureDelegate(figure.InvertZ);

    what is the use if you can just call each one seperatly like normaly? why use delegate if is does the same as calling the fonction.

    Maybe that is not a good example to show the use of delegate. Can someone show me a concrete example easy to understand the use of delegate?

    Thanks

  2. #2
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: C# delegate question

    One of the most common uses of delegates is to access members of a control from a worker thread. You cannot access members of controls in any thread other than the UI thread. In this case you create a delegate, whcih can cross the thread boundary and invoke a method on the UI thread, thus allowing you to access control members.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  3. #3
    Join Date
    Dec 2005
    Posts
    282

    Re: C# delegate question

    delegates are also used for callback function and events

    Regards
    Hansjörg

  4. #4
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: C# delegate question

    And of course you can call a method asynchronously using BeginInvoke and EndInvoke.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured