2013-10-18 84 views
4

我想學習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?

+3

想知道你是否[設置了斷點並通過調試器運行](http://msdn.microsoft.com/en-us/library/vstudio/ 4607yxb0(v = VS.100)的.aspx)?你可以學到很多關於你的邏輯流程,以及爲什麼事情沒有這樣做。在這裏提問完全可以,請注意,只是在您不知道如何操作時提供小費。 – David

+0

經過9個小時的工作和學習,我的大腦被炸,我完全忘記了通過代碼。感謝您的提醒! – user2405778

回答

7

在設置hasParty之前,您正在設置MyProperty。每次調用MyProperty時都不會調用getMessage()

+0

非常感謝你,我不相信那只是那個。我以爲我瘋了。當網站允許我時,我會接受你的回答。 – user2405778

+2

是的,這是正確的答案。此外,這整個MyProperty的想法並不是一個好的實現。使用此類的函數應該能夠將每個屬性視爲關聯的字段或對象。如果您正在設置,MyProperty代表一個人的姓名,但如果您正在接受生日消息,則代表一個生日消息。相反,請創建一個名稱屬性和一個消息屬性。該消息將有一個getter,但沒有setter,並且getter使用您設置的Name值。如果沒有設置名稱,它可能會給出不同的信息,如「誰?」 –

+1

感謝您的解釋@Mark,我確定有更好的方法來編碼它,但我正在儘可能地遵循本教程。我開始意識到,儘管它很有幫助,但我會更多地從我自己的項目中學習,並通過編碼自己的東西來學習。 – user2405778

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