Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:30:13+00:00 2026-06-18T12:30:13+00:00

I have a beginner’s question on Scope. In the MainWindow class I’ve created an

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:30:14+00:00Added an answer on June 18, 2026 at 12:30 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am python beginner and have a question.. I have a class function looks
i have a beginner question. Im making a video site. I store the data
R Beginner here. I have a list of class references. Each class has a
I have this question in my beginner Java programming class and I can't find
I'm new to Java and I have a beginner question: NumberFormat is an abstract
Absolute beginner question: I have a template file index.html that looks like this: ...
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner question: I have a dictionary where the values are lists of (a variable
I have a beginner question with URL Dispatch or Traversal. What I am trying
I have another beginner's question that hopefully someone can help with. I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.