Create a constructor that public Employee(string eName, accepts a first name and decimal wage, double hours) { last name EmployeeName = eName; HourlyWage = wage; HoursWorked = hours; } Note the three parameters, one for each member, same type as the corresponding member For each parameter, the parameter is sent to the corresponding property (the Set method of the property). This ensures any validation included in the property (set) is also applied to the values sent to the constructor. The parameters are sent from the (the form), user class therefore, they should be validated. The parameter names can be anything (could have used e, w, h) I often use abbreviated versions of the property names like I did above. Both of these constructors can be included in the same class. Classes often have numerous constructors to give the many options for instantiating a class. user class If a class has more than one constructor, the constructors must have different The signatures. signature is comprised of the data types (not the names) of the parameters. In these examples, the first constructor has no parameters and the second receives The signatures are obviously different so both can be included. If a constructor that includes parameters exists, the Rewrite the no-parameter constructor with no parameters can usually be simplified constructor by the parameterized constructor. invoking this("", public Employee() : 0m, 0){ } The constructor uses to to the other chain this constructor. It then provides 3 parameters which are sent to the other constructor (the one that accepts three parameters of those types). Note the required set of empty curly brackets at the end
public Employee(string eName, decimal wage, double hours) { EmployeeName = eName; HourlyWage = wage; HoursWorked = hours; } Note the three parameters, one for each member, same type as the corresponding member For each parameter, the parameter is sent to the corresponding property (the Set method of the property). This ensures any validation included in the property (set) is also applied to the values sent to the constructor. The parameters are sent from the user class (the form), therefore, they should be validated. The parameter names can be anything (could have used e, w, h) I often use abbreviated versions of the property names like I did above. Create a constructor that accepts a first name and last name Both of these constructors can be included in the same class. Classes often have numerous constructors to give the user class many options for instantiating a class. If a class has more than one constructor, the constructors must have different signatures. The signature is comprised of the data types (not the names) of the parameters. In these examples, the first constructor has no parameters and the second receives The signatures are obviously different so both can be included. If a constructor that includes parameters exists, the constructor with no parameters can usually be simplified by invoking the parameterized constructor. public Employee() : this("", 0m, 0){ } Rewrite the no-parameter constructor The constructor uses this to chain to the other constructor. It then provides 3 parameters which are sent to the other constructor (the one that accepts three parameters of those types). Note the required set of empty curly brackets at the end
Unit%201%20-%20Class%20Basics.docx