将true值传递给布尔值

时间:2013-10-18 20:19:30

标签: c# constructor boolean

我正在尝试学习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(即使我的理解是正确的,应该是默认值),但是当我尝试设置it = true时,程序无法识别它。我应该以不同的方式传递布尔值,然后我会使用字符串或int吗?

2 个答案:

答案 0 :(得分:7)

您在设置MyProperty之前设置了hasPartygetMessage()。

答案 1 :(得分:0)

MyProperty的工作方式令人困惑,因为setget处理不同的值(您set名称,然后get整个消息,这是令人困惑的)。我将其替换为GivenName属性,然后制作GetMessage()(或将其公开为只读属性Messagepublic

此外,您可以使用auto-properties使代码更简单(您可以使用private get来保持只写行为,但在现实世界中,只写属性非常罕见,你可能应该像set s那样公开它们。由于默认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; }
}