+ 1

POO C# : Test the value in a setter

Hello, I want to test the value of an attribute and to modify another attribute. Is it possible to do this? public class Informaticien { private string _fonction; private int _nombrePC; private string _testerNombrePc; public string Fonction { get { return _fonction; } set { _fonction = value; } } public int NombrePC { get { return _nombrePC; } set { if(value > 0) { _nombrePC = value; _testerNombrePc = "Valeur incorrecte"; } else { _testerNombrePc = "Valeur incorrecte"; } } }

6th Aug 2017, 4:21 PM
Attitude
Attitude - avatar
9 Réponses
+ 1
Sorry I did not read your code entirely. AfficherInfoUnePersonne(); does request the value of TestNombrePc; This was a really good puzzle. The point is do you set the value of a property in the constructor or in the setter. this._nombrePC = nombrePC; Is setting the value of the private variable _nombrePC but does not call the setter and there fore does not set the value of _testerNombrePC. to set the value of _testerNombrePC call the setter method. 1) You set the value after the object is created p2.NombrePC = 4; or 2) use the public variable that hold the set method in the constructor this.NombrePC = nombrePC; Thank you, I have enjoyed myself.
8th Aug 2017, 7:49 PM
sneeze
sneeze - avatar
+ 1
It's okey now. I modify the code and it works fine. But if you have commentaries you can write some. Thank you. Here is my code: public class Informaticien:Personne { private string _fonction; private int _nombrePC; private string _testerNombrePc; public string Fonction { get { return _fonction; } set { _fonction = value; } } public int NombrePC { get { if (_nombrePC >= 1 && _nombrePC <= 2) { _testerNombrePc = "Valeur correcte"; } else { _testerNombrePc = "Valeur incorrecte"; } return _nombrePC; } set { _nombrePC = value; } }
6th Aug 2017, 4:35 PM
Attitude
Attitude - avatar
+ 1
Hello Sneeze, I see that it's not possible to test the value in the setter. It does not show the value of the attirbute _testerNombrePc.
7th Aug 2017, 2:14 AM
Attitude
Attitude - avatar
+ 1
Hello Sneeze, I tested yours it works fine. But mine no. I share the code https://code.sololearn.com/c48qO3JsoMXk Thank you
8th Aug 2017, 2:22 AM
Attitude
Attitude - avatar
+ 1
I modified the code. In the Main i created an object of the class Informaticien. But after runing it in it doesn't show the value of the attribute _testerNombrePC. Here's the code: https://code.sololearn.com/c48qO3JsoMXk#cs
8th Aug 2017, 10:22 AM
Attitude
Attitude - avatar
0
This is not the way I should do it. In your code I can set any number of PC. If you then request if the number was correct, it gives me the result. Much to late. Something like this. Test the value to set, in the setter. set { if (value >= 1 && value <= 2) { _nombrePC = value; _testerNombrePc = "Valeur correcte"; } else { _testerNombrePc = "Valeur incorrecte"; } }
6th Aug 2017, 8:32 PM
sneeze
sneeze - avatar
0
Not true. I did write the code to show it is possible to check the value in the setter. It does not show the value of _testerNombrePC because you do not request for it. Have a look at the following code and play with it. Notice the getter is only called when you want to show its value. https://code.sololearn.com/cuzempurRiSa If you have a good check in the setter, you do not need a property that shows that the value was correct.
7th Aug 2017, 8:02 PM
sneeze
sneeze - avatar
0
Well done. What is wrong ? You have to create a instance of your class. Your main is empty. In my Main I have created Member1 and Member2. A class is just a blue print of the object to be, it does not do anything.
8th Aug 2017, 5:19 AM
sneeze
sneeze - avatar
0
You have created the object. Well done. But _testerNombrePc is a private string. You have to create a property for it, to show its value. Or add a Console.WriteLine in the setter. (not for real world programming but very use full for testing purposes) At this moment there is no code that is asking for _testNombrePC to show.
8th Aug 2017, 7:17 PM
sneeze
sneeze - avatar