.NET 4.x Ошибка при наследовании с использованием закрытых переменных экземпляров - C#
Формулировка задачи:
При изучении наследования с использованием закрытых переменных экземпляров появилась ошибка, относящаяся к строке в файле BasePlusCommissionEmployeeTest.cs:
Visual Studio выдает:
FormatException was unhandled
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Код программы:
В чем может быть ошибка?
Console.WriteLine("\n{0}:\n\n{l}", "Updated employee information obtained bу ToString", employee);
//BasePlusCommissionEmployee.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UseVarExs { public class BasePlusCommissionEmployee : CommissionEmployee { private decimal baseSalary; public BasePlusCommissionEmployee(string first, string last, string ssn, decimal sales, decimal rate, decimal salary) :base(first, last, ssn,sales,rate) { baseSalary = salary; } public decimal BaseSalary { get { return baseSalary; } set { if (value >= 0) baseSalary = value; else throw new ArgumentOutOfRangeException( "BaseSalary", value, "baseSalary must be >= 0"); } } public override decimal Earnings() { return BaseSalary + base.Earnings(); } public override string ToString() { return string.Format("base-sales {0}\nbase salary: {1:C}", base.ToString(), BaseSalary); } } }
// CommissionEmployee.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UseVarExs { public class CommissionEmployee { private string firstName; private string lastName; private string socialSecurityNumber; private decimal grossSales; private decimal commissionRate; public CommissionEmployee(string first, string last, string ssn, decimal sales, decimal rate) { firstName = first; lastName = last; socialSecurityNumber = ssn; GrossSales = sales; CommissionRate = rate; } public string FirstName { get { return firstName; } } public string LastName { get { return lastName; } } public string SocialSecurityNumber { get { return socialSecurityNumber; } } public decimal GrossSales { get { return grossSales; } set { if (value >= 0) grossSales = value; else throw new ArgumentOutOfRangeException( "GrossSales", value, "Grosssales must be >=0"); } } public decimal CommissionRate { get { return commissionRate; } set { if (value > 0 && value < 1) commissionRate = value; else throw new ArgumentOutOfRangeException( "CommissionRate", value, "CommissionRate must be > 0 & <1"); } } public virtual decimal Earnings() { return CommissionRate * GrossSales; } public override string ToString() { return string.Format( "{0}: {1} {2}\n{3}: {4}\n{5}: {6:C}\n{7}: {8:F2}", "commission employee", FirstName, LastName, "social security number", SocialSecurityNumber, "gross sales", GrossSales, "commission rate", CommissionRate); } } }
//BasePlusCommissionEmployeeTest.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UseVarExs { public class BasePlusCommissionEmployeeTest { static void Main(string[] args) { BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee("Bob", "Lewis", "333-33-3333", 5000.00M, .04M, 300.00M); Console.WriteLine( "Employee information obtained Ьу properties and methods: \n"); Console.WriteLine("First name is {0}", employee.FirstName); Console.WriteLine("Last name is {0}", employee.LastName); Console.WriteLine("Social security number is {0}", employee.SocialSecurityNumber); Console.WriteLine("Gross sales are {0:С}", employee.GrossSales); Console.WriteLine("Commission rate is {0:F2}", employee.CommissionRate); Console.WriteLine("Earnings are {0:С}", employee.Earnings()); Console.WriteLine("Base salary is {0:С}", employee.BaseSalary); employee.BaseSalary = 1000.00M; Console.WriteLine("\n{0}:\n\n{l}", "Updated employee information obtained Ьу ToString", employee); Console.WriteLine("earnings: {0:С}", employee.Earnings()); Console.ReadKey(); } } }
Решение задачи: «.NET 4.x Ошибка при наследовании с использованием закрытых переменных экземпляров»
textual
Листинг программы
Console.OutputEncoding = System.Text.Encoding.UTF8;
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д