4

私は C# を学習しようとしていますが、ブール値を使用する例に取り組んでいます。私の人生では、真の値をブール値に渡そうとしていることにプログラムが気付かない理由を理解できません。Form.cs のコードは次のとおりです。

namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        HappyBirthday birthdayMessage = new HappyBirthday();
        string returnedMessage;

        birthdayMessage.PresentCount = 5;
        birthdayMessage.MyProperty = "Adam";
        birthdayMessage.hasParty = true;
        returnedMessage = birthdayMessage.MyProperty;

        MessageBox.Show(returnedMessage);

    }
}
}

作成したクラスは次のとおりです。

class HappyBirthday
{

//====================
//  CLASS VARIABLES
//====================
private int numberOfPresents;
private string birthdayMessage;
private bool birthdayParty;

//===========================
//  DEFAULT CONSTRUCTOR
//===========================
public HappyBirthday()
{
    numberOfPresents = 0;
    //birthdayParty = false;
}

//===========================
//      METHOD
//===========================
private string getMessage(string givenName)
{

    string theMessage;

    theMessage = "Happy Birthday " + givenName + "\n";
    theMessage += "Number of presents = ";
    theMessage += numberOfPresents.ToString() + "\n";

    if (birthdayParty == true)
    {
        theMessage += "Hope you enjoy the party!";
    }
    else
    {
        theMessage += "No party = sorry!";
    }

    return theMessage;
}

//================================
//      READ AND WRITE PROPERTY
//================================
public string MyProperty
{
    get { return birthdayMessage; }

    set { birthdayMessage = getMessage(value); }
}

//================================
//     WRITE-ONLY PROPERTY
//================================
public int PresentCount
{
    set { numberOfPresents = value; }
}

public bool hasParty
{
    set { birthdayParty = value; }
}

}

ここで、初期値を false に設定します (私の理解が正しければ、それがデフォルト値である必要があります) が、それを true に設定しようとすると、プログラムはそれを認識しません。ブール値を別の方法で渡すことになっていて、文字列または整数を渡す必要がありますか?

4

2 に答える 2

7

設定MyPropertyする前に設定していますhasParty。 がポーリングgetMessage()されるたびに呼び出されるわけではありません。MyProperty

于 2013-10-18T20:22:42.587 に答える
0

と は異なる値を処理するため (名前とメッセージ全体が混乱するため)、動作MyPropertyがわかりにくくなっています。私はそれをプロパティに置き換えてから、を作成します(または読み取り専用プロパティとして公開します)。setgetsetgetGivenNameGetMessage()Messagepublic

また、 auto-propertiesを使用してコードをよりシンプルにすることもできます ( private gets を使用して書き込み専用の動作を維持できますが、現実の世界では書き込み専用プロパティは非常にまれであり、おそらくそれらを public にする必要がありますsets)。また、デフォルトint値は0であるため、デフォルト コンストラクターを指定する必要はありません。コードは次のようになります。

class HappyBirthday
{
    public string Message
    {
        get
        {
            string theMessage;

            theMessage = "Happy Birthday " + GivenName + "\n";
            theMessage += "Number of presents = ";
            theMessage += PresentCount.ToString() + "\n";

            if (HasParty)
            {
                theMessage += "Hope you enjoy the party!";
            }
            else
            {
                theMessage += "No party = sorry!";
            }

            return theMessage;
        }
    }

    public string GivenName { private get; set; }

    public int PresentCount { private get; set; }

    public bool HasParty { private get; set; }
}
于 2013-10-18T20:29:40.843 に答える