我是初学程序员,了解C#。当我在Visual Studio中将它与我的主.cs链接并开始调试时,我的.dll上一直出现堆栈溢出错误。我的.dll代码如下:
// Begin namespace LibEmployee1
namespace LibEmployee1
{
/****************************************************************/
/* 1. Class Employee */
/****************************************************************/
public class Employee
{
private const double FICA_RATE = 0.07;
private const double FED_TAX_RATE = 0.22;
private const double STATE_TAX_RATE = 0.05;
private int id;
private string lastName,firstName;
private Date hireDate;
private double rate;
private double hours;
public Employee() // Default constructor
{
this.id = 0;
this.lastName = "";
this.firstName = "";
this.hireDate = new Date();
this.rate = 0.0;
this.hours = 0.0;
}
public Employee(int idValue, string lastNameValue, // Initializing constructor
string firstNameValue, Date hireDateValue,
double rateValue, double hoursValue)
{
this.ID = idValue;
this.LastName = lastNameValue;
this.FirstName = firstNameValue;
this.HireDate = new Date(hireDateValue);
this.Rate = rateValue;
this.Hours = hoursValue;
}
public Employee(int idValue, string lastNameValue, // Initializing constructor
string firstNameValue, int hireYearValue,
int hireMonthValue, int hireDayValue,
double rateValue, double hoursValue)
{
this.ID = idValue;
this.LastName = lastNameValue;
this.FirstName = firstNameValue;
this.hireDate = new Date(hireYearValue,hireMonthValue,hireDayValue);
this.Rate = rateValue;
this.Hours = hoursValue;
}
public Employee(int idValue, string lastNameValue, // Initializing constructor
string firstNameValue, string hireDateString,
double rateValue, double hoursValue)
{
this.ID = idValue;
this.LastName = lastNameValue;
this.FirstName = firstNameValue;
this.hireDate = Date.Parse(hireDateString);
this.Rate = rateValue;
this.Hours = hoursValue;
}
public Employee(Employee sourceEmployee) // Copy constructor
{
this.hireDate = new Date();
this.Copy(sourceEmployee);
}
public int ID // Define read/write ID property
{
get
{
return this.id;
}
set
{
if ((value >= 1) && (value <= 9999))
this.id = value;
else
ProcessError(String.Format("{0} can not be assigned to an ID property\n\nAbort?", value));
}
}
public string LastName // Define read/write LastName property
{
get
{
return this.lastName;
}
set
{
this.lastName = value.Trim();
}
}
public string FirstName // Define read/write FirstName property
{
get
{
return this.firstName;
}
set
{
this.firstName = value.Trim();
}
}
public Date HireDate // Define read/write HireDate property
{
get
{
return this.HireDate;
}
set
{
this.HireDate.Copy(value);
}
}
public double Rate // Define read/write Rate property
{
get
{
return this.Rate;
}
set
{
if ((value >= 0) && (value <= 9999.0))
this.Rate = value;
else
ProcessError(String.Format("{0} can not be assigned to the Rate property\n\nAbort?", value));
}
}
public double Hours // Define read/write Hours property
{
get
{
return this.hours;
}
set
{
if ((value >= 0) && (value <= 300.0))
this.hours = value;
else
ProcessError(String.Format("{0} can not be assigned to the Hours property\n\nAbort?", value));
}
}
public double Earnings
{
get
{
return Math.Round(this.Rate * this.hours, 2);
}
}
public double FICA // Define result property
{
get
{
return Math.Round(this.Earnings * .07, 2);
}
}
public double FedTax // Define result property
{
get
{
return Math.Round(this.Earnings * .22, 2);
}
}
public double StateTax // Define result property
{
get
{
return Math.Round(this.Earnings * .05, 2);
}
}
public double NetPay // Define result property
{
get
{
return Math.Round(this.Earnings - (this.FICA + this.FedTax + this.StateTax), 2);
}
}
public string this[int propertyIndex] // Define readonly indexer property
{
get
{
string returnValue = "";
switch (propertyIndex)
{
case 0: returnValue = this.id.ToString("d4"); break;
case 1: returnValue = this.lastName; break;
case 2: returnValue = this.firstName; break;
case 3: returnValue = this.hireDate.ToString("MM/dd/yyyy"); break;
case 4: returnValue = this.Earnings.ToString("f2"); break;
case 5: returnValue = this.FICA.ToString("f2"); break;
case 6: returnValue = this.FedTax.ToString("f2"); break;
case 7: returnValue = this.StateTax.ToString("f2"); break;
case 8: returnValue = this.NetPay.ToString("f2"); break;
default: ProcessError(String.Format("{0} is an invalid " +
"propertyIndex value for the Employee " +
"class this[] indexer\n\nAbort?", propertyIndex)); break;
}
return returnValue;
}
}
public string this[string propertyName] // Define readonly indexer property
{
get
{
string returnValue = "";
switch (propertyName.ToUpper())
{
case "ID" : returnValue = this.id.ToString("d4"); break;
case "LASTNAME" : case "LAST NAME": returnValue = this.lastName; break;
case "FIRSTNAME": returnValue = this.firstName; break;
case "HIREDATE" : returnValue = this.hireDate.ToString("MM/dd/yyyy"); break;
case "EARNINGS" : returnValue = this.Earnings.ToString("f2"); break;
case "FICA" : returnValue = this.FICA.ToString("f2"); break;
case "FEDTAX" : returnValue = this.FedTax.ToString("f2"); break;
case "STATETAX" : returnValue = this.StateTax.ToString("f2"); break;
case "NETPAY" : returnValue = this.NetPay.ToString("f2"); break;
default: ProcessError(String.Format("{0} is an invalid " +
"propertyName value for the Student " +
"class this[] indexer\n\nAbort?", propertyName)); break;
}
return returnValue;
}
}
public void Copy(Employee sourceEmployee) // Copy method
{
this.id = sourceEmployee.id; this.rate = sourceEmployee.rate;
this.lastName = sourceEmployee.lastName; this.firstName = sourceEmployee.firstName;
this.hours = sourceEmployee.hours;
this.hireDate.Copy(sourceEmployee.hireDate);
}
public Employee Clone()
{
return new Employee
(this);
}
public int CompareTo(Employee employee)
{
return this.id.CompareTo(employee.id);
}
public static int CompareIDs(Employee employee1, Employee employee2)
{
return employee1.id.CompareTo(employee2.id);
}
public static int CompareNames(Employee employee1, Employee employee2)
{
string string1 = employee1.lastName + employee1.firstName + employee1.id.ToString("d4");
string string2 = employee2.lastName + employee2.firstName + employee2.id.ToString("d4");
return string1.CompareTo(string2);
}
public static int CompareHireDates(Employee employee1, Employee employee2)
{
string string1 = employee1.lastName + employee1.firstName + employee1.hireDate.ToString("");
string string2 = employee2.lastName + employee2.firstName + employee2.hireDate.ToString("");
return string1.CompareTo(string2);
}
public static int CompareEarnings(Employee employee1, Employee employee2)
{
string string1 = employee1.lastName + employee1.firstName + employee1.Earnings.ToString("");
string string2 = employee2.lastName + employee2.firstName + employee2.Earnings.ToString("");
return string1.CompareTo(string2);
}
public static Employee Parse(string stringValue)
{
string[] words;
Employee employee = new Employee();
stringValue = StringMethods.SpaceDelimit(stringValue);
words = stringValue.Split(' ');
employee.ID = Int32.Parse(words[0]);
employee.LastName = words[1];
employee.FirstName = words[2];
employee.HireDate = Date.Parse(words[3]);
employee.Rate = Double.Parse(words[4]);
employee.Hours = Double.Parse(words[5]);
return employee;
}
public override string ToString()
{
return String.Format("{0:d4} {1,-15} {2,-15} {3,10:MM/dd/yyyy} {4,7:f} {5,7:f} {6,7:f} {7,7:f} {8,7:f} {9,7:f} {10,7:f}",
this.id, this.lastName, this.firstName, this.HireDate,
this.rate, this.hours, this.Earnings, this.FICA, this.FedTax,
this.StateTax, this.NetPay);
}
private void ProcessError(string message)
{
DialogResult result;
result = MessageBox.Show(message, "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
if (result==DialogResult.Yes)
ConsoleApp.Exit();
}
} // End class Employee
/****************************************************************/
/* 2. Class EmployeeList : IEnumerable */
/****************************************************************/
public class EmployeeList : IEnumerable
{
public delegate int CompareDelegate(Employee employee1, Employee employee2);
private int capacity; // Data member
private int count; // Data member
private Employee[] items; // Data member
public EmployeeList() // Default constructor
{
this.capacity = 1;
this.count = 0;
this.items = new Employee[1];
}
private EmployeeList(int capacityValue) // Initializing constructor
{
if (capacityValue>=1)
{
this.capacity = capacityValue;
this.count = 0;
this.items = new Employee[capacityValue];
}
else
ProcessError(String.Format("{0} can not be the capacity of an EmployeeList object\n\nAbort?",capacityValue));
}
public EmployeeList(EmployeeList sourceList) // Copy constructor
{
this.Copy(sourceList);
}
public int Count // Define read-only Count property
{
get
{
return this.count;
}
}
public Employee this[int index] // Define read/write this[] indexer property
{
get
{
if ((index>=1) && (index<=count))
return this.items[index-1];
else
{
ProcessError(String.Format("EmployeeList [] Get index must be between 1 and {0}\n\nAbort?",this.count));
return default(Employee);
}
}
set
{
if ((index >= 1) && (index <= this.count))
this.items[index - 1] = value;
else
ProcessError(String.Format("EmployeeList [] Set index must be between 1 and {0}\n\nAbort?", this.count));
}
}
public bool Empty // Return true (false) if list is (not) empty
{
get
{
return (this.count == 0);
}
}
public IEnumerator GetEnumerator() // IEnumerable Interface Implementation:
{ // Declaration of the GetEnumerator() method,
for (int i=1; i<=this.count; i++) // which is needed to give meaning to the "foreach"
yield return this[i]; // control construct.
}
public void Clear() // Remove all list elements
{
this.capacity = 1;
this.count = 0;
this.items = new Employee[1];
}
public void Copy(EmployeeList sourceList) // This approach is correct
{
this.Clear();
foreach (Employee employee in sourceList)
this.Add(employee.Clone());
}
public EmployeeList Clone()
{
return new EmployeeList(this);
}
private void IncreaseCapacity()
{
EmployeeList tempList;
tempList = new EmployeeList(2 * this.capacity);
foreach (Employee employee in this)
tempList.Add(employee);
this.capacity = tempList.capacity;
this.count = tempList.count;
this.items = tempList.items;
}
public void Add(Employee employee)
{
this.InsertAt(this.count+1, employee);
}
public void InsertAt(int position, Employee employee)
{
int i;
if ((position >= 1) && (position <= this.count + 1))
{
if (this.count == this.capacity)
this.IncreaseCapacity();
this.count++;
for (i = this.count; i > position; i--)
this[i] = this[i - 1];
this[position] = employee;
}
else
ProcessError(String.Format("EmployeeList InsertAt index must be between 1 and {0}\n\nAbort?", this.count + 1));
}
public Employee RemoveAt(int position)
{
int i;
Employee employee = null;
if ((position >= 1) && (position <= this.count))
{
employee = this[position];
for (i = position; i < this.count; i++)
this[i] = this[i + 1];
this.count--;
}
else
ProcessError(String.Format("EmployeeList RemoveAt index must be between 1 and {0}\n\nAbort?", this.count));
return employee;
}
private void ProcessError(string message)
{
DialogResult result;
result = MessageBox.Show(message, "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
ConsoleApp.Exit();
}
public void Reverse()
{
int i;
Employee tempValue;
for (i = 1; i <= this.count / 2; i++)
{
tempValue = this[i];
this[i] = this[this.count + 1 - i];
this[this.count + 1 - i] = tempValue;
}
}
public int Locate(Employee employee) // Locate and return the list index
{ // of data in the list.
int listIndex = 1;
while ((listIndex <= this.count) && (this[listIndex].CompareTo(employee) < 0))
listIndex++;
if (listIndex > this.count)
listIndex = ~listIndex;
else if (this[listIndex].CompareTo(employee) > 0)
listIndex = ~listIndex;
return listIndex;
}
public int Locate(Employee employee, CompareDelegate compareMethod) // Locate and return the list index
{ // of data in the list.
int listIndex = 1; // Uses a CompareDelegate method.
while ((listIndex <= this.count) && (compareMethod(this[listIndex], employee) < 0))
listIndex++;
if (listIndex > this.count)
listIndex = ~listIndex;
else if (compareMethod(this[listIndex], employee) > 0)
listIndex = ~listIndex;
return listIndex;
}
public void Sort() // Uses the Employee class CompareTo method
{
int i, j, k;
Employee temp;
for (i = 1; i <= (this.count - 1); i++)
{
k = i;
for (j = (i + 1); j <= this.count; j++)
if (this[j].CompareTo(this[k]) < 0)
k = j;
if (k > i)
{
temp = this[k];
this[k] = this[i];
this[i] = temp;
}
}
}
public void Sort(CompareDelegate compareMethod) // Uses a CompareDelegate method.
{
int i, j, k;
Employee temp;
for (i = 1; i <= (this.count - 1); i++)
{
k = i;
for (j = (i + 1); j <= this.count; j++)
if (compareMethod(this[j], this[k]) < 0)
k = j;
if (k > i)
{
temp = this[k];
this[k] = this[i];
this[i] = temp;
}
}
}
public double Total(string propertyName)
{
double total = 0.0;
foreach (Employee employee in this)
total += Double.Parse(employee[propertyName]);
return total;
}
public double Mean(string propertyName)
{
return this.Total(propertyName) / this.Count;
}
public double Max(string propertyName)
{
int i;
double max = Double.Parse(this[1][propertyName]);
for (i = 2; i <= this.Count; i++)
if (Double.Parse(this[i][propertyName]) > max)
max = Double.Parse(this[i][propertyName]);
return max;
}
public double Min(string propertyName)
{
int i;
double min = Double.Parse(this[1][propertyName]);
for (i = 2; i <= this.Count; i++)
if (Double.Parse(this[i][propertyName]) < min)
min = Double.Parse(this[i][propertyName]);
return min;
}
public int AssignID()
{
int returnValue = 1;
if (this.count>0)
returnValue = (int) this.Max("ID") + 1;
return returnValue;
}
public void Input(StreamReader fileIn)
{
string lineIn;
while ((lineIn=fileIn.ReadLine())!=null)
this.Add(Employee.Parse(lineIn));
}
public void PrintReport(StreamWriter fileOut, string orderDescriptor)
{
int indent = (63 - orderDescriptor.Length) / 2;
orderDescriptor = "".PadLeft(indent) + orderDescriptor;
fileOut.WriteLine(" Employee Report ");
fileOut.WriteLine(orderDescriptor);
fileOut.WriteLine(" {0:MM/dd/yyyy}", Date.Today);
fileOut.WriteLine();
fileOut.WriteLine(" Jon Ernst ");
if (!this.Empty)
{
fileOut.WriteLine(" ID Last Name First Name Hired Earnings FICA Fed Tax State Tax Net Pay ");
fileOut.WriteLine("---- --------------- --------------- ---------- --------- --------- --------- --------- ---------");
foreach (Employee employee in this)
fileOut.WriteLine("{0:d4} {1,-15} {2,-15} {3,10:MM/dd/yyyy} {4,7:f} {5,7:f} {6,7:f} {7,7:f} {8,7:f}",
employee.ID, employee.LastName, employee.FirstName, employee.HireDate,
employee.Earnings, employee.FICA, employee.FedTax, employee.StateTax, employee.NetPay);
fileOut.WriteLine(" --------- -------- --------- --------- ---------");
fileOut.WriteLine("{0,-52} {1,7:f} {2,7:f} {3,7:f} {4,7:f} {5,7:f}", "Mean", this.Mean("Earnings"), this.Mean("FICA"), this.Mean("Fed Tax"), this.Mean("State Tax"), this.Mean("Net Pay"));
fileOut.WriteLine("{0,-52} {1,7:f} {2,7:f} {3,7:f} {4,7:f} {5,7:f}", "Maximum", this.Max("Earnings"), this.Max("FICA"), this.Max("Fed Tax"), this.Max("State Tax"), this.Max("Net Pay"));
fileOut.WriteLine("{0,-52} {1,7:f} {2,7:f} {3,7:f} {4,7:f} {5,7:f}", "Minimum", this.Min("Earnings"), this.Min("FICA"), this.Min("Fed Tax"), this.Min("State Tax"), this.Min("Net Pay"));
fileOut.WriteLine();
fileOut.WriteLine("Count = {0}", this.Count);
}
else
fileOut.WriteLine("The list is empty.");
fileOut.WriteLine();
}
} // End class EmployeeList
} // End namespace LibEmployee1
Visual Studio在此方法中指出了堆栈溢出错误:
public Date HireDate // Define read/write HireDate property
{
get
{ **<=== visual studio points the overflow error specifically to this line**
return this.HireDate;
}
set
{
this.HireDate.Copy(value);
}
}
答案 0 :(得分:1)
区分大小写问题。您以递归方式调用属性getter。返回this.hireDate 此外,无需在设置器中复制日期。这是一种价值类型。
答案 1 :(得分:0)
您的代码中有答案,在您的HireDate
媒体资源中,您将返回HireDate
媒体资源的价值,而后者又会返回您HireDate
的值属性等等..!您可能希望返回支持字段的值。
public DateTime HireDate
{
get
{
return this.hireDate;
}
set
{
this.hireDate = value;
}
}