James C
James C

Reputation: 27

How do I refer to a class instance created in the MainWindow from another class

I have a beginner's question on Scope. In the MainWindow class I've created an instance of the ModelView class for data binding, and an instance of the Cabbage class which has the Leaves property I want to display. My question is how do I refer to myCabbage.Leaves from the updateCabbageLeaves method?

namespace TestScope
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ModelView myModelView = new ModelView();
        Cabbage myCabbage = new Cabbage();
        myCabbage.Leaves = 99;
        myModelView.updateCabbageLeaves();
    }
}

class ModelView
{
    public int CabbageLeaves { get; set; }

    public void updateCabbageLeaves()
    {
        //CabbageLeaves = myCabbage.Leaves;
    }
}

class Cabbage
{
    public int Leaves { get; set; }
}

}

I guess this is not the way to set up a MVVM. My reasoning is: for a MVVM I need a class for the model and a class for the model view. The application starts in the MainWindow class so I've created instances of the model and the model view classes there.

I'm fairly new to C#. Thanks for any help.

James

Upvotes: 1

Views: 129

Answers (2)

David L
David L

Reputation: 33833

Your perspective of the relationship isn't quite right. What you really want to do is create an instance of the Cabbage model itself within your view model, pass it in, and map and manipulate it there.

NOTE: Your classes should also be public. You must specify this.

namespace TestScope
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ModelView myModelView = new ModelView();
            Cabbage myCabbage = new Cabbage();
            myCabbage.Leaves = 99;
            myModelView.updateCabbageLeaves(myCabbage);
        }
    }

    public class ModelView
    {
        public Cabbage Cabbage { get; set; }

        public void updateCabbageLeaves(Cabbage myCabbage)
        {
            Cabbage = myCabbage.Leaves;
        }
    }

    public class Cabbage
    {
         public int Leaves { get; set; }
    }
}

To be honest however, I don't care much for the manipulation of Cabbage in your main method at all. You could instead pass in the amount of leaves that you want in a constructor and allow the view model to handle itself.

In main...

var myModelView = new ModelView(99);

And in your view model

public class ModelView
{
    public ModelView(int numberOfLeaves)
    {
        Cabbage = new Cabbage(){ Leaves = numberOfLeaves }
    }

    public Cabbage Cabbage { get; set; }
}

This is a little cleaner and doesn't force manipulation of Cabbage in your main method

Upvotes: 3

Igoy
Igoy

Reputation: 2962

You can pass the cabbage object as an arguement:

public void updateCabbageLeaves(Cabbage myCabbage)
    {
        this.CabbageLeaves = myCabbage.Leaves;
    }

 public MainWindow()
    {
        InitializeComponent();

        ModelView myModelView = new ModelView();
        Cabbage myCabbage = new Cabbage();
        myCabbage.Leaves = 99;
        myModelView.updateCabbageLeaves(myCabbage);
    }

Upvotes: 0

Related Questions