Delegates
-
hi , i wrote this code so that i can understand delegates... code works fine ....good but when i changed the code a bit ie when i wrote the main function inside another class if i need to call the method using delegates then i have to call like ""mydelegate obj = new mydelegate(class.dog);"" ORIGINAL CODE:: class Program { public static void dele() { Console.WriteLine("the delegate will call this function"); Console.ReadLine(); } static void Main(string[] args) { mydelegate obj = new mydelegate(dog); obj(); } } } ALETRED CODE WITH TWO CLASSES:: class Program { public static void dele() { Console.WriteLine("the delegate will call this function"); Console.ReadLine(); } class Program2 { public static void anotherdele() { static void Main(string[] args) { mydelegate obj = new mydelegate(Program.dog); obj(); } } } SO My doubt here is i thought using delegates we need not know the name of the class but here if the main is enclosed in another class i need to use the class name.... any help is welcome ....... thanks
C#
-
hi , i wrote this code so that i can understand delegates... code works fine ....good but when i changed the code a bit ie when i wrote the main function inside another class if i need to call the method using delegates then i have to call like ""mydelegate obj = new mydelegate(class.dog);"" ORIGINAL CODE:: class Program { public static void dele() { Console.WriteLine("the delegate will call this function"); Console.ReadLine(); } static void Main(string[] args) { mydelegate obj = new mydelegate(dog); obj(); } } } ALETRED CODE WITH TWO CLASSES:: class Program { public static void dele() { Console.WriteLine("the delegate will call this function"); Console.ReadLine(); } class Program2 { public static void anotherdele() { static void Main(string[] args) { mydelegate obj = new mydelegate(Program.dog); obj(); } } } SO My doubt here is i thought using delegates we need not know the name of the class but here if the main is enclosed in another class i need to use the class name.... any help is welcome ....... thanks
C#
IF you are referencing a static method the class must be known, if you are using an instance method, the instance should be known (and definately NOT null).
xacc.ide
The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."