5

I have 2 classes both non-static. I need to access a method on one class to return an object for processing. But since both classes is non-static, I cant just call the method in a static manner. Neither can I call the method in a non-static way because the program doesnt know the identifier of the object.

Before anything, if possible, i would wish both objects to remain non-static if possible. Otherwise it would require much restructuring of the rest of the code.

Heres the example in code

class Foo
{
    Bar b1 = new Bar();

    public object MethodToCall(){ /*Method body here*/ }
}

Class Bar
{
    public Bar() { /*Constructor here*/ }

    public void MethodCaller()
    {
        //How can i call MethodToCall() from here?
    }
}
DarkDestry
  • 183
  • 1
  • 11
  • 3
    Basically you want to do something like `new Foo().MethodToCall();` I suppose... – atlaste Dec 15 '14 at 15:22
  • 1
    You need an object of class `Foo` in `Bar` class and then call the *instance* method on that. Or you can pass a parameter of type `Foo` to your method in `Bar` class. – Habib Dec 15 '14 at 15:22
  • 1
    `Neither can I call the method in a non-static way because the program doesnt know the name of the object.` wut –  Dec 15 '14 at 15:24
  • 5
    You do know you can make the method static but not the class, right? – Alexandre Pepin Dec 15 '14 at 15:26
  • 1
    Even though what you ask is possible, it hardly makes any sense. Consider making adding a static overload on the class. This way you could make a static call and a regular call, depending on the situation. – Victor Zakharov Dec 15 '14 at 15:28
  • This would make sense if each Foo is an object you forsee requires resetting. If you would reset Foo constantly, it would make sense to contain Bar inside Foo in which when you create a new instance, all references to the old instance is void. This is the reason i want to refrain from using static methods and classes. – DarkDestry Dec 15 '14 at 15:48
  • possible duplicate of [Why can't I call a public method in another class?](http://stackoverflow.com/questions/330138/why-cant-i-call-a-public-method-in-another-class) – StuartLC Jan 10 '15 at 06:51

4 Answers4

11
class Bar
{
    /*...*/

    public void MethodCaller()
    {
        var x = new Foo();
        object y = x.MethodToCall();
    }
}

BTW, in general, objects don't have names.

lorond
  • 3,856
  • 2
  • 37
  • 52
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • This would work but it will create a new instance of Foo, which i dont want to happen. The variables in Foo would still remain in the old instance and the new instance `x` would have a fresh set of variables. Also, im new with the terminologies and i mean to say reference/identifier instead of name :P – DarkDestry Dec 15 '14 at 15:38
2

By passing an instance to the constructor:

class Bar
{
    private Foo foo;

    public Bar(Foo foo)
    { 
        _foo = foo;
    }

    public void MethodCaller()
    {
        _foo.MethodToCall();
    }
}

Usage:

Foo foo = new Foo();
Bar bar = new Bar(foo);
bar.MethodCaller();
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
2

In order for any code in a static or a non-static class to call a non-static method, the caller must have a reference to the object on which the call is made.

In your case, Bar's MethodCaller must have a reference to Foo. You could pass it in a constructor of Bar or in any other way you like:

class Foo
{
    Bar b1 = new Bar(this);

    public object MethodToCall(){ /*Method body here*/ }
}

Class Bar
{
    private readonly Foo foo;

    public Bar(Foo foo) {
        // Save a reference to Foo so that we could use it later
        this.foo = foo;
    }

    public void MethodCaller()
    {
        // Now that we have a reference to Foo, we can use it to make a call
        foo.MethodToCall();
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Try this:

class Foo
{
    public Foo() { /*Constructor here*/ }
    Bar b1 = new Bar();

    public object MethodToCall(){ /*Method body here*/ }
}

Class Bar
{
    public Bar() { /*Constructor here*/ }
    Foo f1 = new Foo();
    public void MethodCaller()
    {
        f1.MethodToCall();
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • This would recreate a new instance of Foo inside Bar. I want to process the object inside Foo and not inside a new instance of Foo. – DarkDestry Dec 15 '14 at 15:42