Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have a problem with my Code. Some classes shouldn't be possible to write in propertys but some classes should be able to write and read them.

Problem in detail:
I coded the invoice in the Controller as private set, so i can only set the invoice in this class. that is what i want.
I also want that i can set the properties of the model in the controller, but not in the view.
But if i set the properties not public in the model, i cant set the properties in the controller too.

I have got 4 classes they looks like the model-view-controller:

Models:
C#
namespace payOffice.Model
{
    class InvoicePosition
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }
}


C#
namespace payOffice.Model
{
    class Invoice
    {
        public List<InvoicePosition> Positions { get; set; }
        public string Consumer { get; set; }
        public bool Completed { get; set; }
        public int InvoiceNumber { get; set; }

        public Invoice(string consumer)
        {
            Consumer = consumer;
        }
    }
}


Controller:
C#
namespace payOffice.Controller
{
    class InvoiceManager
    {
        public Invoice loadedInvoice {get;private set;}

        public InvoiceManager(...){...}

        public void addInvoicePosition(...)
        {
            //here it should be possible:
            loadedInvoice.Positions.Add(...);
        }
    }
}


View:
C#
namespace payOffice.View
{
    public partial class MyView : Form
    {
        InvoiceManager invoiceManager;
        public MyView()
        {
            InitializeComponent();
        }

        public void addInvoicePosition(...)
        {
            //its right so:
            invoiceManager.addInvoicePosition(...);

            //that shouldn't be possible!!!:
            invoiceManager.loadedInvoice.Positions.Add(...);

            //but that should be possible in other methods:
            Console.WriteLine(invoiceManager.loadedInvoice.Consumer);
            //and this should be possible in other methods too
            Console.WriteLine(invoiceManager.loadedInvoice.Positions[0].Price);
        }

    }
}


Did someone have an idea how i can make this possible?
Posted
Updated 31-Jul-14 23:47pm
v3
Comments
Kumarbs 1-Aug-14 4:49am    
You used private setter, i think that is the problem. Ensure that.
Member 10401272 1-Aug-14 4:55am    
I see that my problem isn't clear. i improved the question and added a detailed question.
Philippe Mori 2-Aug-14 8:22am    
If you don't want to allows properties to be modified, the best solution is to have private set and use constructor (or static method) to initialize all fields from parameters.

That way, properties can only be set at construction time (similar to DateTime or string classes in .NET).

At the moment, all of your properties are public - which means anyone can use them if they know teh class exists.

But that's OK, because all your classes are independent, in different namespaces, and nothing is derived from anything else.
So even using protected or internal modifiers on your properties won't help, because the namespaces and lack of derivation mean there are effectively private anyway.

Basically, with the structure you have, you can't prevent any classes from accessing properties if any classes can.

You need to take a step back, look at what you are trying to do, and consider a better class structure (heck - any class structure) and / or better encapsulation to restrict access.

With what you have designed so far, they really isn't anything you can do.
 
Share this answer
 
Comments
Member 10401272 1-Aug-14 4:58am    
Do you think it is better to do the manager/controller in the model namespace and set the getter internal?


That should be the solution, or?

btw. this is only a example of my big "payOffice"code :D
C#
hi, you have to remove either the setter or the getter.

class InvoicePosition
    {
        private string name { get; set; }    //switch the visibility to private, and the name //start with lowerCase 
//same with this variable
        private double price { get; set; }   //same here

//you can use a property named : [Name] to accecs your var [name]
 public string Name
        {
            get { return name; } // this ligne allow you to read the value
            set { name = value; } //// this ligne allow you to write into the variable
        }
 public double Price  
        {
            get { return price; }
        }
// => Price is only accescable for read.

    }




}

if you work with VS, then encapsualte and the code will be generated.

Hope it helps.
 
Share this answer
 

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