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

4 回答 4

5

您不能在 C# 中的类之外声明方法。

于 2013-10-25T11:15:35.040 回答
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;
        }
    }
}
于 2013-10-25T11:29:17.953 回答
0

先去掉那个}

它导致它下面的属性出现在类之外。

于 2013-10-25T11:19:42.737 回答
-1

成员目前不在您的班级内。_uploadPicture 后面的大括号;} 应该在最后。

此外,还有一些其他错误:

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

}

于 2013-10-25T11:23:59.500 回答