C # methods to overload Constructor

Source: Internet
Author: User

I encountered a little trouble in my work today. concerning the problem of constructor overloading, I used to directly use the same function name to input different parameters during method overloading. The following code is used:

 1     public class UserData
2 {
3
4
5 public bool UpdateUser(string username, string password, int age, int sex,int id)
6 {
7 return true;
8 }
9
10 public bool UpdateUser(string username,int id)
11 {
12 return UpdateUser(username, "", 0, 0,id);
13 }
14 }

When the constructor is overloaded, the above method will not work and an error will be reported.

The reason is that I will not explain much. constructors are used for instantiation. After several attempts, I finally found a solution.

1 public class user
2 {
3 /// <summary>
4 // Initialize an empty user class instance.
5 /// </Summary>
6 public user ()
7 {
8
9}
10
11 /// <summary>
12 // initialize a user-class instance that contains user information.
13 /// </Summary>
14 /// <Param name = "username"> User Name </param>
15 /// <Param name = "password"> password </param>
16 /// <Param name = "Age"> age </param>
17 // <Param name = "sex"> gender </param>
18 public user (string username, string password, int age, int sex)
19 {
20 This. _ username = username;
21 This. _ password = password;
22 This. _ age = age;
23 This. _ sex = sex;
24}
25
26 /// <summary>
27 /// initialize a user-class instance that contains the user name and password.
28 /// </Summary>
29 // <Param name = "username"> </param>
30 /// <Param name = "password"> </param>
31 public user (string username, string password)
32: This (username, password, 0, 0)
33 {
34}
35
36 private int _ id;
37 private string _ username;
38 private string _ password;
39 private int _ age;
40 private int _ sex;
41
42
43 public int ID
44 {
45 get {return _ id ;}
46 set {_ id = value ;}
47}
48
49 public int sex
50 {
51 get {return _ sex ;}
52 set {_ sex = value ;}
53}
54
55 public int age
56 {
57 get {return _ age ;}
58 set {_ age = value ;}
59}
60
61 Public String Password
62 {
63 get {return _ password ;}
64 set {_ password = value ;}
65}
66
67 Public String Username
68 {
69 get {return _ username ;}
70 set {_ username = value ;}
71}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.