napravio sam klasu HappyBirthday,
u njoj myProperty,
i metodu getMessage.
Code (csharp):
class HappyBirthday
{
private string birthdayMessage;
public string myProperty
{
get { return birthdayMessage; }
set { birthdayMessage = getMessage(false);}
}
public string getMessage(bool haveParty)
{
string theMessage;
if (haveParty == true)
{
theMessage = "Enjoy your party!";
}
else
theMessage = "Sory-no party for you !";
return theMessage;
}
}
class HappyBirthday
{
private string birthdayMessage;
public string myProperty
{
get { return birthdayMessage; }
set { birthdayMessage = getMessage(false);}
}
public string getMessage(bool haveParty)
{
string theMessage;
if (haveParty == true)
{
theMessage = "Enjoy your party!";
}
else
theMessage = "Sory-no party for you !";
return theMessage;
}
}
u klik dogadjaju za button1 kontrolu dodao sam kod:
Code (csharp):
private void button1_Click(object sender, EventArgs e)
{
string poruka;
HappyBirthday hb = new HappyBirthday();
hb.myProperty = true.ToString();
poruka = hb.myProperty;
MessageBox.Show(poruka);
}
private void button1_Click(object sender, EventArgs e)
{
string poruka;
HappyBirthday hb = new HappyBirthday();
hb.myProperty = true.ToString();
poruka = hb.myProperty;
MessageBox.Show(poruka);
}
ovaj kod radi, ali bez obzira da li napisao
hb.myProperty = true.ToString(); ili
hb.myProperty = false.ToString();
on prikazuje u messageboxu:
Sory-no party for you !
to je verovatno zato sto u set delu property-ja pise:
set { birthdayMessage = getMessage(false);}
sta treba da se promeni u kodu da bi u button1_click dogadjau mogao da menjam vrednost property-ja
kako bi mi u zavisnosti od toga da li postavim vrednost na true ili false bile prikazane razlicite poruke u
message box-u ?
[Ovu poruku je menjao Shadowed dana 10.06.2011. u 11:44 GMT+1]