Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

Nothing can be declared outside of a Class...

 

A delegate has the "scope" of the class. If you declare the class and the delegate as public then they have, more or less, global scope. Meaning, any class would be able to create a delegate of that type.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

but here the delegate is declared outside of the class:

-------------------------------

public delegate void MyDelegate;

public class MyClass {

public event MyDelegate triggerMyDelegate;

}

-------------------------------

And can be used in any other class (of that namespace?) as well.

Debug me...
Posted
I think this can be done only if MyClass is an inner class

 

 

No because I'm using it constantly in my project and it currently contains no inner classes at all. Since the delegate uis turned ionto a class by the compiler it makes sence not to declare the delegate as part of a certain class.

As the delegate can be declared in any class and used (e.g. implemented as an event) in all other classes I was just wondering what the standard procedure for declaring delegates is:

When I'm using a delegate in 10 classes but only declare it in one of them it might be hard to find that one class that declares it. Also I don't know what impact the delegate has towards namespaces.

 

Maybe it's differentin VB ?

I'm pretty new to delegates but I know the code above is working just fine in my app.

Debug me...
  • *Experts*
Posted

My mistake - only coded delegates a few time, long ago and quickly forgot how :)

 

Delegates are basically "global". If you define your delegate outside of a namespace then it's truly global. If you define it inside of a namespace, you'll have to reference the namespace when declare an instance of the delegate.

 

For example, say you had ClassLibrary1:

using System;
public delegate void MyDelegate();
namespace ClassLibrary1
{
}

 

And ConsoleApplication1:

using System;
namespace ConsoleApplication1
{
class Class1
{
	[sTAThread]
	static void Main(string[] args)
	{
		MyDelegate mydelegate = new MyDelegate(test);
		mydelegate();
		Console.Read();
	}
	static void test()
	{
		Console.WriteLine("Hello World");
	}
}
}

 

This will work. Now if you change ClassLibrary1 to put the delegate inside of the namespace like so:

using System;
namespace ClassLibrary1
{
   public delegate void MyDelegate();
}

 

Now ConsoleApplication1 won't compile unless you give it the namespace of ClassLibrary1, as in:

using System;
namespace ConsoleApplication1
{
class Class1
{
	[sTAThread]
	static void Main(string[] args)
	{
		[b]ClassLibrary1.[/b]MyDelegate mydelegate = new [b]ClassLibrary1.[/b]MyDelegate(test);
		mydelegate();
		Console.Read();
	}
	static void test()
	{
		Console.WriteLine("Hello World");
	}
}
}

 

Obviously, this assumes the delegate is made public. If it were internal, for example, it wouldn't be accessible outside the project/assembly.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...