Home > OS >  When using the classname() call to create a new object does this not pass by the setter property in
When using the classname() call to create a new object does this not pass by the setter property in

Time:11-08

I was under the impression that when calling a new object that new object is built using the classes setters and thus we can put constraints on in the setter property to make sure the object is formated and within the boundries we want them to be.

    public class Commune{
        private string name;
        private int department;
        private string country;
        private string mayor;
        private int population;

        public Commune(string n, int d, string c, string m, int p)
        {
            name = n;
            department = d;
            country = c;
            mayor = m;
            population = p;
        }
        public string Name
        {
            get { return name; }
            set { name = value.ToUpper(); }
        }
        public int Department
        {
            get { return department; }
            set {
                if(value <= 0){
                    department = -1;
                }else{
                    department = value;    
                }    
            }
        }
        public string Country
        {
            get { return country; }
            set { country = value; }
        }
        public string Mayor
        {
            get { return mayor; }
            set { mayor = value.ToLower(); }
        }
        public int Population{
            get{ return population;}
            set {
                if(value <= 0){
                    population = -1;
                }else{
                    population = value;    
                }    
            }
        }
}

I coded my class "Commune" with certain rules for the properties, Name and Mayors should be formated to be in Upper and Lower respectively and the department number and population should be positive.

But when I declare a new object.

//string name, int department, string country, string mayor, int population
Commune test = new Commune("name", -5, "count", "may", -5);

It creates and object without any of the formating. Is there any way around this or do I have to manually call the setter each time?

CodePudding user response:

There are two ways to achieve your desire.

  1. Changing the constructor
  2. Call the Setter manually

Changing the constructor

public Commune(string n, int d, string c, string m, int p)
    {
        name = n.ToUpper();
        department = d <= 0 ? -1 : d;
        country = c;
        mayor = m.ToLower();
        population = p <= 0 ? -1 : p;  
    }

Call the Setter manually

Commune test = new Commune();
test.Name= "Name"
test.Mayor= "Mayor"

CodePudding user response:

In the code example you're assigning fields in the constructor, not properties. Just assign to properties (ones with capital letters in your example):

    public Commune(string n, int d, string c, string m, int p)
    {
        Name = n;
        Department = d;
        Country = c;
        Mayor = m;
        Population = p;
    }

CodePudding user response:

When you call the constructor, none of the properties are set; you're actually working on the private fields:

public Commune(string n, int d, string c, string m, int p)
        {
            name = n;
            department = d;
            country = c;
            mayor = m;
            population = p;
        }

In order to obtain the formatting you want, initialize the properties instead of the fields (but this would make the fields redundant) or move the formatting logic to the getters of the properties.

  •  Tags:  
  • c#
  • Related