C# 将真值传递给布尔值
我正在努力学习C#,我正在学习一个使用布尔值的例子。就我的一生而言,我无法理解为什么程序没有注意到我试图将true的值传递给布尔值。以下是Form.cs中的代码:C# 将真值传递给布尔值,c#,constructor,boolean,C#,Constructor,Boolean,我正在努力学习C#,我正在学习一个使用布尔值的例子。就我的一生而言,我无法理解为什么程序没有注意到我试图将true的值传递给布尔值。以下是Form.cs中的代码: namespace WindowsFormsApplication7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(o
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时,程序无法识别它。我传递布尔值是否应该与传递字符串或int不同?在设置
hasParty
之前,您正在设置MyProperty
getMessage()
不是每次轮询MyProperty
时都被调用。MyProperty
的工作方式令人困惑,因为set
和get
处理不同的值(您set
名称,然后get
整个消息,这让人困惑)。我会用GivenName
属性替换它,然后将GetMessage()
(或者将其作为只读属性Message
)公开
此外,您还可以通过使用使代码更简单(您可以使用private get
s来保持只写行为,尽管在现实世界中只写属性非常罕见,您可能应该像集
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; }
}
只是想知道你是否?通过这样做,您可以了解很多关于您的逻辑流程以及为什么事情不起作用的信息。提醒你,在这里提问是完全可以的,只是提供一个提示,以防你不知道怎么做。经过9个小时的工作和研究,我的大脑已经崩溃了,我完全忘记了如何处理代码。谢谢你的提醒!太谢谢你了,我真不敢相信只有这一点。我以为我疯了。如果网站允许,我会接受你的答案。是的,这是正确的答案。而且,整个MyProperty的想法并不是一个好的实现。使用此类的函数应该能够将每个属性视为关联的字段或对象。如果您正在设置,则MyProperty表示一个人的姓名,如果您正在收到生日消息,则MyProperty表示生日消息。相反,创建名称属性和消息属性。消息将有一个getter,但没有setter,getter使用您设置的Name值。如果没有设置名称,它可能会给出一个不同的消息,比如“谁?”谢谢你的解释@Mark,我相信有更好的编码方法,但我正在尽可能地遵循本教程。我开始意识到,尽管它很有帮助,但我会从自己的项目中获得更多,并通过编写自己的东西来学习。@user2405778:我也有过一些可能弊大于利的教程。我的建议是密切关注它,直到您正确编译和运行代码,然后重构它,直到您理解它。