comparison with delegates [modified]
-
hi, i have two set of codes here for the same output.... one is by using the functions that i wrote and the other one is by using delegates(got form net) can anybody tell me the advantage of using the delegates in this code?? please check the comments in the code using delegates... i found the normal way much easier.... WITHOUT USING DELEGATES::: using System; using System.Collections.Generic; using System.Text; namespace trialfunctions { class Program { string name; public void enter() { Console.WriteLine("enter a string"); } public void display() { name = (Console.ReadLine()).ToString(); Console.WriteLine("The string entered is {0}", name); Console.ReadLine(); } static void Main(string[] args) { Program obj = new Program(); obj.enter(); obj.display(); } } } ---------------------------------------------------------- /////////////////////////////////////////////////////////// ---------------------------------------------------------- USING DELEGATES::::: using System; public delegate void TestDelegate(string message); //Declare the delegate class Test { public static void Display(string message) { Console.WriteLine("The string entered is {0} " , message); } static void Main() { TestDelegate t = new TestDelegate(Display); //Instantiate the delegate Console.WriteLine("Please enter a string"); string message = Console.ReadLine(); //what is the need for this t(message); //Invoke the delegate Console.ReadLine(); } } thanking you -- modified at 1:45 Thursday 25th October, 2007
C#
-
hi, i have two set of codes here for the same output.... one is by using the functions that i wrote and the other one is by using delegates(got form net) can anybody tell me the advantage of using the delegates in this code?? please check the comments in the code using delegates... i found the normal way much easier.... WITHOUT USING DELEGATES::: using System; using System.Collections.Generic; using System.Text; namespace trialfunctions { class Program { string name; public void enter() { Console.WriteLine("enter a string"); } public void display() { name = (Console.ReadLine()).ToString(); Console.WriteLine("The string entered is {0}", name); Console.ReadLine(); } static void Main(string[] args) { Program obj = new Program(); obj.enter(); obj.display(); } } } ---------------------------------------------------------- /////////////////////////////////////////////////////////// ---------------------------------------------------------- USING DELEGATES::::: using System; public delegate void TestDelegate(string message); //Declare the delegate class Test { public static void Display(string message) { Console.WriteLine("The string entered is {0} " , message); } static void Main() { TestDelegate t = new TestDelegate(Display); //Instantiate the delegate Console.WriteLine("Please enter a string"); string message = Console.ReadLine(); //what is the need for this t(message); //Invoke the delegate Console.ReadLine(); } } thanking you -- modified at 1:45 Thursday 25th October, 2007
C#
In this instance, the delegate is obviously pointless. Delegates are great for creatng components that can be reused, or for comunication between forms.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
In this instance, the delegate is obviously pointless. Delegates are great for creatng components that can be reused, or for comunication between forms.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Yesterday you have pointed that passing current form object to second form through constructor is nasty. Could you tell me why it's so ? If we use delegates also, we need to create object and hook it to a method in form1. So I don't find any differences.
-
In this instance, the delegate is obviously pointless. Delegates are great for creatng components that can be reused, or for comunication between forms.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )