我想學習C#,並且我使用一個布爾值的示例。對於我的生活,我無法弄清楚爲什麼程序沒有注意到我正在嘗試將true的值傳遞給布爾值。下面是在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時,程序無法識別它。我應該傳遞一個布爾值不同,然後我會字符串或int?
想知道你是否[設置了斷點並通過調試器運行](http://msdn.microsoft.com/en-us/library/vstudio/ 4607yxb0(v = VS.100)的.aspx)?你可以學到很多關於你的邏輯流程,以及爲什麼事情沒有這樣做。在這裏提問完全可以,請注意,只是在您不知道如何操作時提供小費。 – David
經過9個小時的工作和學習,我的大腦被炸,我完全忘記了通過代碼。感謝您的提醒! – user2405778