2013-10-25 35 views
-1
namespace MyNamespace 
{ 
    class Student 
    { 
    private string _name; 
    private int _phoneNo; 
    private string _address; 
    private string _occupation; 
    private string _courseOfStudy; 
    private int _duration; 
    private string _uploadPicture; 
    } 

    public string Name 
    { 
    get { return _name;} 
    set { _name = value;} 
    } 

    public int PhoneNumber 
    { 
    get { return _phoneNo;} 
    set { _phoneNo = value;} 
    } 

    public string Address 
    { 
    get { return _address;} 
    set { _address = value;} 
    } 

    public string Occupation 
    { 
    get { return _occupation;} 
    set { _occupation = value;} 
    } 

    public string CourseOfStudy 
    { 
    get { return _courseOfStudy;} 
    set { _courseOfStudy = value;} 
    } 

    public int Duration 
    { 
    get { return _duration;} 
    set { _duration = value;} 
    } 

    public string Uploadpicture 
    { 
    get { return _uploadpicture;} 
    set { _uploadpicture = value;} 
    } 

    public Student() 
    { 
    _name = ""; 
    _phoneNo = ""; 
    _address = ""; 
    _occupation = ""; 
    _courseOfStudy = ""; 
    _duration = ""; 
    _uploadPicture = ""; 

    System.Windows.Forms.MessageBox.Show("Called Constructor") 
    } 

    public Student (String name, int phoneNo, string address, string occupation, string courseOfStudy, int duration, string uploadPicture) 
    { 
    _name = name; 
    _phoneNo = phoneNo; 
    _address = address; 
    _occupation = occupation; 
    _courseOfStudy = courseOfStudy; 
    _duration = duration; 
    _uploadPicture = uploadPicture; 
    } 
} 

回答

5

您不能在C#中的類外部聲明方法。

0

卸下第一}

它導致它下面的屬性出現在類的外部。

-1

這些成員不在你的班級裏。 _uploadPicture後面的大括號; shuld在最後。

此外,還有一個其他一些錯誤:

  • _uploadpicture應_uploadPicture在公共字符串Uploadpicture
  • 你想初始化一個int與一個字符串(_phoneNo = 「」;)

這裏是校正後的類:

namespace MyNamespace { 
class Student { 
    private string _name; 
    private int _phoneNo; 
    private string _address; 
    private string _occupation; 
    private string _courseOfStudy; 
    private int _duration; 
    private string _uploadPicture; 


    public string Name { 
     get { 
      return _name; 
     } 
     set { 
      _name = value; 
     } 
    } 


    public int PhoneNumber { 
     get { 
      return _phoneNo; 
     } 
     set { 
      _phoneNo = value; 
     } 
    } 


    public string Address { 
     get { 
      return _address; 
     } 
     set { 
      _address = value; 
     } 
    } 


    public string Occupation { 
     get { 
      return _occupation; 
     } 
     set { 
      _occupation = value; 
     } 
    } 


    public string CourseOfStudy { 
     get { 
      return _courseOfStudy; 
     } 
     set { 
      _courseOfStudy = value; 
     } 
    } 


    public int Duration { 
     get { 
      return _duration; 
     } 
     set { 
      _duration = value; 
     } 
    } 

    public string Uploadpicture { 
     get { 
      return _uploadPicture; 
     } 
     set { 
      _uploadPicture = value; 
     } 
    } 


    public Student() { 
     _name = ""; 
     _phoneNo = 0; 
     _address = ""; 
     _occupation = ""; 
     _courseOfStudy = ""; 
     _duration = 0; 
     _uploadPicture = ""; 

     System.Windows.Forms.MessageBox.Show("Called Constructor"); 

    } 

    public Student(string name, int phoneNo, string address, string occupation, string courseOfStudy, int duration, string uploadPicture) { 

     _name = name; 
     _phoneNo = phoneNo; 
     _address = address; 
     _occupation = occupation; 
     _courseOfStudy = courseOfStudy; 
     _duration = duration; 
     _uploadPicture = uploadPicture; 
    } 
} 

}

+0

爲什麼你用你的答案對代碼的佈局做了太多改動?由於您的「改進」代碼與OP相比較,因此與原始代碼相比,現在無法讀取。 –

+0

「不可讀的有點苛刻,不是嗎?它是Visual Studio的默認格式 – Turbofant

+0

它絕對不是VS的默認格式。如果VS爲你編寫代碼,那麼你的C#語言設置已經改變了,默認風格是由OP公佈的。 –

0

正如其他人指出的那樣,您已將所有屬性和構造函數放在類之外。此外,您還有大量不必要的代碼來處理屬性,因爲您沒有以任何方式封裝字段值。所以他們可以被重寫爲使用自動屬性。嘗試進行以下重寫:

namespace MyNamespace 
{ 
    class Student 
    { 
     public string Name { get; set; } 

     public int PhoneNumber { get; set; } 

     public string Address { get; set; } 

     public string Occupation { get; set; } 

     public string CourseOfStudy { get; set; } 

     public int Duration { get; set; } 

     public string UploadPicture { get; set; } 

     public Student() : this("", 0, "", "", "", 0, "") 
     { 
      MessageBox.Show("Called Constructor"); 
     } 

     public Student(String name, int phoneNo, string address, string occupation, string courseOfStudy, int duration, string uploadPicture) 
     { 
      Name = name; 
      PhoneNumber = phoneNo; 
      Address = address; 
      Occupation = occupation; 
      CourseOfStudy = courseOfStudy; 
      Duration = duration; 
      UploadPicture = uploadPicture; 
     } 
    } 
} 
相關問題