Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have user control called UCPlayerAccount in where I have created "model", an instance of class PersonModel, and populated it with values.
Now, in UCPlayerAccount i have button that loads another user control, let's say it is called UCX, with it's own logic for creating some tasks for user.
When user confirm's answers, I want to sum points with value from model.Points property, but in UCX I cannot access "model" instance as it is from the previous user control, that is from UCPlayerAccount.
I'm using C# Windows Form.
This is my class PersonModel:
public class PersonModel
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string NickName { get; set; }
        public int Points { get; set; }
        public int CurrLevel { get; set; }
        public int CurrClass { get; set; }
        public List<AvatarPicture> Avatars { get; set; } = new List<AvatarPicture>();


        public string FullName
        {
            get { return $"{ FirstName } { LastName }"; }

        }
    }


What I have tried:

How to access "model" in UCX, can anyone help?
Posted
Updated 12-Sep-20 8:50am

To the extent your goal is not, imho, fully revealed, I am going to show some code that reflects some arbitrary decisions ... in the hope you;ll find the strategy and methods useful :)

I suggest you use a kind of model-view design:
using System.Collections.Generic;
using System.Drawing;

namespace YourNameSpace
{
    public static class Model
    {
        public static Dictionary<UCPlayerAccount, PersonModel> PAcctToPModel =
            new Dictionary<UCPlayerAccount, PersonModel>();
    }

    public class PersonModel
    {
        public PersonModel()
        {
        }

        public PersonModel(int id, string firstName, string lastName, string nickName, int points, int currLevel,
            int currClass, List<Image> avatars)
        {
            Id = id;
            FirstName = firstName;
            LastName = lastName;
            NickName = nickName;
            Points = points;
            CurrLevel = currLevel;
            CurrClass = currClass;
            Avatars = avatars;
        }

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string NickName { get; set; }
        public int Points { get; set; }
        public int CurrLevel { get; set; }
        public int CurrClass { get; set; }
        public List<Image> Avatars { get; set; } = new List<Image>();

        public string FullName => $"{FirstName} {LastName}";
    }
}
The static Model class maps each instance of the UCPlayerAccount to an instance of the PersonModel Class: the Dictionary values are entered when the UCPlayerAccount is initialized(in the Load Event):
C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class UCPlayerAccount : UserControl
    {
        public UCPlayerAccount()
        {
            InitializeComponent();
        }

        public PersonModel PModel { set; get; } = new PersonModel();

        private void UCPlayerAccount_Load(object sender, EventArgs e)
        {
            Model.PAcctToPModel.Add(this, PModel);
        }

        // note no external reference is kept to the instance of UCX
        private void btnOpenUCX_Click(object sender, EventArgs e)
        {
            // reference to PModel injected into new instance of UCX
            UCX ucx = new UCX(PModel);

            this.Controls.Add(ucx);
            ucx.Location = new Point(20,100);
        }
    }
}
I am not sure where you wish the UCX to be placed, so I show it here being created, placed, inside the UCPlayerAccount:
using System
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class UCX : UserControl
    {
        public UCX()
        {
            InitializeComponent();
        }

        // optional constructor
        public UCX(PersonModel pmodel)
        {
            InitializeComponent();
            PModel = pmodel;
        }

        // injected reference
        private PersonModel PModel;

        // test writing to PersonModel
        private void btnWhatever_Click(object sender, EventArgs e)
        {
            if (PModel != null) PModel.Points = 100;
        }
    }
}
Demonstrates how the PersonModel associated with the UCPlayerAccount can be accessed from the UCX.

Since the instance of the UCX is created in the scope of a method in the UCPlayerAccount ... no reference is kept "alive:" ... imho, that is consistent with separation of concerns,

Keeping data models outside Views/Controls pays off, imho, in many ways: for example, the static class 'Model could have a collection of PersonModel that could be serialized, de-serialized, using standard methods.
 
Share this answer
 
v4
Comments
Sandeep Mewara 13-Sep-20 8:01am    
+5
You have "encapsulated" your Person model, so either provide a property to return it from your UserControl, or write a method which returns a specific instance.
 
Share this answer
 
Comments
Member 14922487 12-Sep-20 13:36pm    
Ok, that is something I was thinking about, but can you give me some tip how to do that?
To make it simple, I want to provide UCX with "model".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900