Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 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 November 22nd, 2011, 07:22 AM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default CH13Ex2

Why in any code (this is one example):

namespace ConsoleApplication1
{
public delegate void MessageHandler(string messagetext);

class Connection
{
public event MessageHandler messagearived;
private Timer pooltimer;

public Connection()
{
pooltimer = new Timer(1000);
pooltimer.Elapsed += new ElapsedEventHandler(checkformessage);
}

public void connect()
{
pooltimer.Start();
}

public void dicsonnect()
{
pooltimer.Stop();
}

private static Random random = new Random();
private void checkformessage(object source, ElapsedEventArgs e)
{
Console.WriteLine("checking for new messages");
if ((random.Next(9)==0) && (messagearived !=null))

{
messagearived("hello mum");
}
}
}
}


public delegate void MessageHandler(string messagetext);

preceedes
code in Class.ie Why declaring delegate syntax can't be written inside

Class code:


namespace ConsoleApplication1
{


class Connection
{

public delegate void MessageHandler(string messagetext);

public event MessageHandler messagearived;
private Timer pooltimer;

I put public delegate inside class Connection,and all works fine
 
Old November 22nd, 2011, 09:40 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Declaring a delegate declares a class, and it's possible to put the delegate declaration in the sample classes where a class declaration can be done.
So it's possible to declare a delegate within an outer class, just using the delegate from outside of the class, the name OuterClassName.DelegateName must be used.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
dampyr (November 22nd, 2011)









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