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

前端 未结 4 704
傲寒
傲寒 2021-01-18 16:23

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 metho

4条回答
  •  星月不相逢
    2021-01-18 16:56

    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();
        }
    }
    

提交回复
热议问题