Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. comparison with delegates [modified]

comparison with delegates [modified]

Scheduled Pinned Locked Moved C#
questioncsharp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kabutar
    wrote on last edited by
    #1

    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#

    C 1 Reply Last reply
    0
    • K kabutar

      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#

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      N K 2 Replies Last reply
      0
      • C Christian Graus

        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 )

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        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.


        My Website | Ask smart questions

        1 Reply Last reply
        0
        • C Christian Graus

          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 )

          K Offline
          K Offline
          kabutar
          wrote on last edited by
          #4

          even i thought so..... but i wanted to check.... thanks again :)

          C#

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups