I am trying to figure out how Get and set works, and i am getting a error and i dont really understand why.
constructor with get and set
- class Accounts
- {
- public string Nameinfo;
- public int Ageinfo;
- public Accounts(string aName, int aAge, string aprice)
- {
-
- Nameinfo = aName;
- Ageinfo = aAge;
- Price = aprice;
- }
- public string Price
- {
- get { return Price; }
-
- set {
- if (value == "1" || value == "2" )
- {
- Price = value;
- MessageBox.Show("good");
- }
- else
- {
- value = "0";
- MessageBox.Show("not valid");
- }
- }
- }
- }
Button to activate the constructor
- private void test_Click(object sender, RoutedEventArgs e)
- {
- Accounts accountss = new Accounts("jack", 2, "1");
- MessageBox.Show(accountss.Nameinfo);
-
- accountss.Price = textbox.Text.ToString();
- }