Hi all experts ...
I have a question ....

How can i call a function of Class A from Class B without creating any objects,static menthds or using any inheritance..
like if class A has a variable of classB then classB can call a method of class A....
explanatory code:

class A 
{ 
method A () 
//how to define Var of Class B 
} 
class B 
{ 
// how to call method of class A here
}

Thanks and regards ...

Recommended Answers

All 5 Replies

What exactly are you trying to do just use a function within it? you can inject it as a parameter

Class A 
 {
  public A (B instance)
    {
    B.SomeMethod;
    }
 }

 Class B 
 {
  SomeMethod()
   {
   Console.WriteLine("injected");
   }
 }

or you can use get set method with property look into dependency injection

you can also use an interface too, if for whatever reason you dont wanna inherit it or use an abstract class

Thanks but the code gives compilation error ...

Important thing to add here is that i want to use such way which doesnot require me to call method by writing class name like B.SomeMethod. i want to directly call "SomeMethod;"

try this?

class A
{
    public void methodA()
    {
        Console.Write("This text is by methodA in Class A");
    }
}

class B
{
    public void methodB()
    {
        A Aobj = new A();
        Aobj.methodA();
    }
}

Thanks but i have mentioned earlier that i dont have to use objects or static methods.
i want to implement a logic which doesnot require using classname.methodname rather can be called directly without classname.

Thnaks all....
i have found the answer, that is DELEGATES.
the one who dont know must read to have advanced knowlegde.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.