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
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
Raaj KumarPosted Mar 1, 2010, 1:53 AM
Since the method is static , you cannot refer to a delegate using the instance of class P. Because while creating a new object or instance of the Class P, compiler won't allocate new memory location for the method. Hence if you type objName. the intellisense won't show the method name. This means the new object does not have it's own specific method.
So if you want to achieve this you should make the method as non-static method.
Hope it helps you.
Regards,
raaj
Raaj KumarPosted Mar 1, 2010, 3:44 AM
saravanamurugan mPosted Mar 1, 2010, 2:43 AM
Hellow Raaj,
It's getting work. I have understood, that you have tried to say. Thank you very much........
Saraharisan.M