3 Replies - 927 Views - Last Post: 04 March 2010 - 08:24 PM Rate Topic: -----

#1 saraharisan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-February 10

C# : Unable to Refer a Method of a Class using Delegate

Posted 28 February 2010 - 10:22 PM

Hai Friends,

I am working with Asp.net C#. Here i got a problem with Delegate. I can't able to Refer a method (which is defined in a class) by the class object. Here is my code, Please take a look at it...


Program Code:
using System;

namespace consoleApp {

class baseClass
{

public delegate void delgFun();

public class P 
{

public static void Hellow()
{
Console.WriteLine("Hellow World...");
}

public static void Hai()
{
Console.WriteLine("Hai...");
}

}

public static void Main(String[] args)
{
delgFun delgFunObj1 = P.Hellow;

P objP = new P();
delgFun delgFunObj2 = objP.Hai;        //here comes problem

delgFunObj1();
delgFunObj2();
}

}
}


Please Fix it.

Advanced thanks
Saraharisan. M

This post has been edited by Core: 01 March 2010 - 03:13 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: C# : Unable to Refer a Method of a Class using Delegate

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# : Unable to Refer a Method of a Class using Delegate

Posted 01 March 2010 - 02:50 AM

This is the solution:

public static void Main(String[] args)
{
    delgFun delgFunObj1 = new delgFun(P.Hellow);
    delgFun delgFunObj2 = new delgFun(P.Hai);
    
    //You can use both ways to execute your delegate
    delgFunObj1();
    delgFunObj1.Invoke();

    //You can use both ways to execute your delegate
    delgFunObj2();
    delgFunObj2.Invoke();
}




If you create a new instance of your P class as you did, you can not call the P.Hai method, since your P.Hai method is static.

Hope this helps...
Was This Post Helpful? 1
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# : Unable to Refer a Method of a Class using Delegate

Posted 01 March 2010 - 02:56 AM

If your P class had a non-static method for example:

public class P
{
   public void HaiAgain()
   {
      Console.WriteLine("Hai again...");
   }
}



then you will be able to call it the way you wanted:


public static void Main(String[] args)
{
    P objP = new P();

    delgFun delgFunObj1 = new delgFun(P.Hellow);
    delgFun delgFunObj2 = new delgFun(P.Hai);
    delgFun delgFunObj3 = new delgFun(objP.HaiAgain);

    delgFunObj3();
    
    //You can use both ways to execute your delegate
    delgFunObj1();
    delgFunObj1.Invoke();

    //You can use both ways to execute your delegate
    delgFunObj2();
    delgFunObj2.Invoke();
}


This post has been edited by FlashM: 01 March 2010 - 02:57 AM

Was This Post Helpful? 3
  • +
  • -

#4 saraharisan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-February 10

Re: C# : Unable to Refer a Method of a Class using Delegate

Posted 04 March 2010 - 08:24 PM

Thanks... It's getting work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1