简体   繁体   English

将true值传递给布尔值

[英]Passing a true value to a boolean

I am trying to learn C# and I am up to an example that uses a boolean. 我正在尝试学习C#,我是一个使用布尔值的例子。 For the life of me I cant figure out why the program isnt noticing that I am trying to pass a value of true to the boolean. 对于我的生活,我无法弄清楚为什么程序没有注意到我试图将值传递给布尔值。 Here is the code in the Form.cs: 这是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);

    }
}
}

Here is the Class that I created: 这是我创建的类:

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; }
}

}

Now I set the initial value to false (even though if my understanding is correct that should be the default value), but when I try to set it = true, the program does not recognize it. 现在我将初始值设置为false(即使我的理解是正确的,应该是默认值),但是当我尝试设置it = true时,程序无法识别它。 Am I supposed to pass a boolean differently then I would a string or int? 我应该以不同的方式传递布尔值,然后我会使用字符串或int吗?

You're setting MyProperty before you're setting hasParty . 您在设置hasParty之前设置了MyProperty getMessage() is not being called every time MyProperty is polled. 每次轮询MyProperty都不会调用getMessage()

The way MyProperty works is confusing, because the set and get deal with different values (you set the name, and then get the whole message, which is confusing). MyProperty工作方式令人困惑,因为setget处理不同的值(你set名称,然后get整个消息,这是令人困惑的)。 I'd replace it with a GivenName property and then make the GetMessage() (or expose it as a read-only property Message ) public . 我将它替换为GivenName属性,然后将GetMessage() (或将其作为只读属性Message publicpublic

Also, you can make your code much simpler by using auto-properties (you can use private get s to keep the write-only behavior, though in the real world write-only properties are very rare, and you should probably just make them public like the set s). 此外,您可以通过使用自动属性使您的代码更简单(您可以使用private get来保持只写行为,但在现实世界中,只写属性非常罕见,您可能应该将它们公开像set s)。 And since the default int value is 0 , you don't need to specify your default constructor. 由于默认的int值为0 ,因此您无需指定默认构造函数。 Here's how the code looks now: 以下是代码现在的样子:

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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM