The method creates an object and I call the method from another object

If a method creates an object and I call the method from another object, will the last object have access to the first properties and methods of the object?

0


a source to share


5 answers


There is some extraneous information that may confuse you.



The method and the object (in this case) are detached from each other. So the question is, are you storing the created object in a scope that the second object has access to?

+1


a source


In the router, in the way, in the included file is the controller code. into the model, and in my opinion I need access to controller properties and methods. In the router, in another method, I want to return the controller object to the model and view. What Now ??

If I understand the question correctly, you are a little confused about MVC. The router class is the inner class of the cake and should never ever be absolutely never changed. And those "never" are not even copied, they are actually printed.

Second, the model classes don't even know anything called a controller. The controller uses models, not the other way around. If your model needs something from the controller, pass it as a parameter. Anything behind this is just bad design.



Also, calling controller actions from a view is possible, but strongly discouraged. The controller is the one that prepares all the data for the view, so the view doesn't need to access the controller (there are exceptions to this, outside the scope of this question).

I recommend you read a little about MVC , cake a typical request, and at least go through a basic blogging tutorial .

+1


a source


If it is returned / saved anywhere, public fields and methods will be available.

0


a source


Edit: In light of the tag change, this answer is no longer relevant. I left it to save comments ...

Original answer:

Like this?:

public MyObject CreateObject()
{
   return new MyObject() { FirstProperty = "Hello World" };
}

public Main()
{
    MyObject n = CreateObject();
    Console.WriteLine(n.FirstProperty);
}

      

Or that?:

class Program
{
    MyObject _myObject;

    public void CreateObject()
    {
        _myObject = new MyObject() { FirstProperty = "Hello World" };
    }

    public Main()
    {
        Console.WriteLine(_myObject.FirstProperty);
    }
}

      

In either of these two cases, you can access the properties of your object. If that's not what you meant, I don't know exactly how to answer your question, and you need to clarify.

0


a source


Only if the method retains a reference to the object it creates.

0


a source







All Articles