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. Delegates

Delegates

Scheduled Pinned Locked Moved C#
csharphelp
2 Posts 2 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 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#

    L 1 Reply Last reply
    0
    • K kabutar

      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#

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      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."

      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