2016-02-02 52 views
-3

我已經上課了,這是我製作的地址簿。 我在製作公共地址類對象變量時遇到了麻煩。我不知道如何編碼它。如何在C#中創建公共類級別的對象變量?

namespace Add_New_Name 
{ 
    class Address 
    { 
     public string strName; 
     public string strEmailAddress; 
     public string strComment; 
     public string strPhoneNumber; 
     public string strAddress; 


     //property Name 
     #region Property Name 
     public string Name 
     { 
      get 
      { 
       return strName; 
      } 
      set 
      { 
       strName = value; 
      } 
     } //end property Address 
     #endregion 

     //property Email Address 
     #region Email Address 
     public string EmailAddress 
     { 
      get 
      { 
       return strEmailAddress; 
      } 
      set 
      { 
       strEmailAddress = value; 
      } 
     } //end property Email Address 
     #endregion 

     //property Comment 
     #region Comment 
     public string Comment 
     { 
      get 
      { 
       return strComment; 
      } 
      set 
      { 
       strComment = value; 
      } 
     }//end property Comment 
     #endregion 

     //property Phone Number 
     #region Phone Number 
     public string PhoneNumber 
     { 
      get 
      { 
       return strPhoneNumber; 
      } 
      set 
      { 
       strPhoneNumber = value; 
      } 
     } 
     #endregion 

     //property Address 
     #region Address 
     public string address 
     { 
      get 
      { 
       return strAddress; 
      } 
      set 
      { 
       strAddress = value; 
      } 
     } 
     #endregion 

     //constructor 
     #region Constructor 
     public Address (string sName, string sEmailAddress, string sComment, string sPhoneNumber, string sAddress) 
     { 
      Name = sName; //property name 
      EmailAddress = sEmailAddress; //property address 
      Comment = sComment; // property comment 
      PhoneNumber = sPhoneNumber; // property phone number 
      address = sAddress; // property address 

     } 
     #endregion 
    } 
} 
+5

「我無法使公共地址級別對象變量「 - Ca你是否可以改變這個?你可以給我們一小段代碼,它直接關係到你正在使用的問題(而不是整個班級) – Rob

回答

1

嘗試改變

class Address 

public class Address 

然後,您可以創建這樣的對象,以便(編輯,以顯示與參數的構造函數):

var myAddress = new Address("me", "email", "code is cool", "a number", "an address"); 
+0

,這不起作用我得到一大堆紅線 – Ice

+1

請注意,該類沒有無參數構造函數... –

+0

@Ice當您將鼠標懸停在紅線上時,出現了哪些錯誤消息(添加到您的問題中,而不是註釋中)。 –