How can a non-static class call another non-static class's method?
Asked Answered
S

4

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?
    }
}
Sauter answered 15/12, 2014 at 15:21 Comment(7)
Basically you want to do something like new Foo().MethodToCall(); I suppose...Unstressed
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.Hypersonic
Neither can I call the method in a non-static way because the program doesnt know the name of the object. wutHokum
You do know you can make the method static but not the class, right?Borreri
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.Strobotron
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.Sauter
possible duplicate of Why can't I call a public method in another class?Monteria
A
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();
    }
}
Audriaaudrie answered 15/12, 2014 at 15:24 Comment(0)
L
11
class Bar
{
    /*...*/

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

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

Leuctra answered 15/12, 2014 at 15:23 Comment(1)
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 :PSauter
K
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();
Kazak answered 15/12, 2014 at 15:23 Comment(0)
A
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();
    }
}
Audriaaudrie answered 15/12, 2014 at 15:24 Comment(0)
W
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();
    }
}
Wichern answered 15/12, 2014 at 15:26 Comment(1)
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.Sauter

© 2022 - 2024 — McMap. All rights reserved.