Malfunction Posted April 27, 2004 Posted April 27, 2004 (edited) Where do you usually declare delegates? I know they're declared outside classes but what scope do they have? Edited April 27, 2004 by Malfunction Quote Debug me...
*Experts* Nerseus Posted April 27, 2004 *Experts* Posted April 27, 2004 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 Quote "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
Malfunction Posted April 27, 2004 Author Posted April 27, 2004 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. Quote Debug me...
wessamzeidan Posted April 28, 2004 Posted April 28, 2004 I think this can be done only if MyClass is an inner class Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
Malfunction Posted April 28, 2004 Author Posted April 28, 2004 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. Quote Debug me...
*Experts* Nerseus Posted April 28, 2004 *Experts* Posted April 28, 2004 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 Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.