-2个

我是一名初学者,学习C#。当我在Visual Studio中将.dll链接到我的main.cs并开始调试时,我的.dll上总是出现堆栈溢出错误。我的.dll代码如下:

//开始命名空间LibEmployee1命名空间LibEmployee1{/****************************************************************//* 1. 班级员工*//****************************************************************/公共级员工{私有常量双FICA_RATE=0.07;私有常量双FED_TAX_RATE=0.22;私有常量双STATE_TAX_RATE=0.05;私有int id;私有字符串lastName,firstName;私人日期租用日期;私人双费率;私人双人小时;public Employee()//默认构造函数{this.id=0;this.lastName=“”;this.firstName=“”;this.hireDate=新日期();this.rate=0.0;this.hours=0.0;}public Employee(int idValue,string lastNameValue,//初始化构造函数字符串firstNameValue,Date hireDateValue,双倍费率值,双倍小时值){这个。ID=idValue;这个。LastName=lastNameValue;这个。FirstName=firstNameValue;这个。HireDate=新日期(hireDateValue);这个。Rate=rateValue;这个。小时=小时值;}public Employee(int idValue,string lastNameValue,//正在初始化构造函数字符串firstNameValue,int hireYearValue,int hireMonthValue,int hireDayValue,双倍费率值,双倍小时值){这个。ID=idValue;这个。LastName=姓氏值;这个。FirstName=firstNameValue;this.hireDate=新日期(hireYearValue、hireMonthValue和hireDayValue);这个。Rate=rateValue;这个。小时=小时值;}public Employee(int idValue,string lastNameValue,//初始化构造函数字符串firstNameValue,字符串hireDateString,双倍费率值,双倍小时值){这个。ID=idValue;这个。LastName=姓氏值;这个。FirstName=firstNameValue;this.hireDate=日期。解析(hireDateString);这个。Rate=rateValue;这个。小时=小时值;}public Employee(员工源员工)//复制构造函数{this.hireDate=新日期();这个。副本(sourceEmployee);} public int ID//定义读/写ID属性{得到{返回this.id;}设置{if((值>=1)&&(值<=9999))this.id=值;其他的ProcessError(String.Format(“{0}无法分配给ID属性\n\nAbort?”,value));}}公共字符串LastName//定义读/写LastName属性{得到{return this.lastName;}设置{this.lastName=值。修剪();}}公共字符串FirstName//定义读/写FirstName属性{得到{return this.firstName;}设置{this.firstName=值。修剪();}}public Date HireDate//定义读/写HireDateproperty{得到{返回这个。招聘日期;}设置{这个。雇佣日期。副本(值);}}公共双速率//定义读/写速率属性{得到{返回这个。费率;}设置{if((值>=0)&&(值<=9999.0))这个。比率=值;其他的ProcessError(String.Format(“{0}无法分配给Rate属性\n\nAbort?”,value));}}public double Hours//定义读/写Hours属性{得到{返回this.hours;}设置{if((值>=0)&&(值<=300.0))this.hours=值;其他的ProcessError(String.Format(“{0}无法分配给Hours属性\n\nAbort?”,值);}}公众双重收益{得到{return数学。四舍五入(this.Rate*this.hours,2);}}public double FICA//定义结果属性{得到{return数学。轮次(this.收益*.07,2);}}public double FedTax//定义结果属性{得到{return数学。轮次(this.收益*.22,2);}}public double StateTax//定义结果属性{得到{return数学。轮次(this.收益*.05,2);}}public double NetPay//定义结果属性{得到{return数学。四舍五入(this.收益-(this.FICA+this.FedTax+this.StateTax),2);}}公共字符串this[int propertyIndex]//定义只读索引器属性{得到{string returnValue=“”;开关(属性索引){案例0:returnValue=this.id.ToString(“d4”);断裂;案例1:returnValue=this.lastName;断裂;案例2:returnValue=this.firstName;断裂;案例3:returnValue=this.hireDate。ToString(“MM/dd/yyyy”);断裂;案例4:returnValue=此。收益。ToString(“f2”);断裂;案例5:returnValue=this。国际公务员协。ToString(“f2”);断裂;情况6:returnValue=这个。联邦税务局。ToString(“f2”);断裂;案例7:returnValue=this。州税。ToString(“f2”);断裂;案例8:returnValue=this。NetPay。ToString(“f2”);断裂;默认值:ProcessError(String.Format(“{0}无效”+“员工的propertyIndex值”+“class this[]indexer \n\nAbort?”,propertyIndex);断裂;}return returnValue;}}public string this[string propertyName]//定义只读索引器属性{得到{string returnValue=“”;开关(propertyName.ToUpper()){案例“ID”:returnValue=this.ID.ToString(“d4”);断裂;case“LASTNAME”:case“姓氏”:returnValue=this.LASTNAME;断裂;case“FIRSTNAME”:returnValue=this.FIRSTNAME;断裂;case“HIREDATE”:returnValue=this.HIREDATE。ToString(“MM/dd/yyyy”);断裂;case“EARNINGS”:returnValue=这个。收益。ToString(“f2”);断裂;案例“FICA”:returnValue=此。国际公务员协。ToString(“f2”);断裂;案例“FEDTAX”:returnValue=this。联邦税务局。ToString(“f2”);断裂;case“STATETAX”:returnValue=this。州税。ToString(“f2”);断裂;case“NETPAY”:returnValue=这个。NetPay。ToString(“f2”);断裂;默认值:ProcessError(String.Format(“{0}无效”+“学生的propertyName值”+“classthis[]索引器\n\nAbort?”,propertyName));断裂;}return returnValue;}}public void Copy(Employee sourceEmployer)//复制方法{this.id=源员工id;this.rate=源员工费率;this.lastName=源员工姓氏;this.firstName=源员工名字;this.hours=sourceEmployee.hours;this.hire日期。副本(sourceEmployee.hireDate);}公共雇员克隆(){返回新员工(本);}public int CompareTo(员工){                                         return this.id.CompareTo(employee.id);}public static int CompareID(雇员employee1,雇员emplonee2){return employee1.id.CompareTo(employere2.id);}public static int CompareNames(Employee employee1,Employer employere2){string string1=员工1.lastName+员工1.firstName+员工2.id.ToString(“d4”);string string2=员工2.lastName+员工2.firstName+员工2.id.ToString(“d4”);return string1.CompareTo(string2);}public static int CompareHireDates(Employee employee1,Employer employere2){string string1=employee1.lastName+employere1.firstName+embloyee1.雇用日期。ToString(“”);string string2=employee2.lastName+employere2.firstName+embloyee2.雇用日期。ToString(“”);return string1.CompareTo(string2);}public static int CompareIncomes(employee1,Employme2){string string1=employee1.lastName+employere1.firstName+embloyee1.收入。ToString(“”);string string2=employee2.lastName+employere2.firstName+empleoyee2.收入。ToString(“”);return字符串1.CompareTo(字符串2);}public static Employee Parse(string stringValue){字符串[]单词;员工=新员工();stringValue=字符串方法。空格分隔符(字符串值);words=字符串值。拆分(“”);员工。ID=Int32.解析(单词[0]);员工。姓氏=单词[1];员工。名字=单词[2];员工。HireDate=日期。解析(单词[3]);员工。费率=双倍。解析(单词[4]);员工。小时=双倍。解析(单词[5]);返聘员工;}公共重写字符串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:fneneneep{8,7:f}{9,7:f{10,7:f2.6”,this.id、this.lastName、this.firstName、this。雇佣日期,这个日期,这个小时,这个。收益,这一点。FICA,这个。联邦税务局,这个。州税,这个。NetPay);}private void ProcessError(字符串消息){DialogResult结果;结果=MessageBox。显示(消息,“错误”,MessageBoxButtons.YesNo,MessageBoxIcon.Error,MessageBox默认按钮。按钮1);if(结果==DialogResult.Yes)控制台应用程序。退出();}}//结束类员工/****************************************************************//* 2. 类员工列表:IEnumerable*//****************************************************************/公共类EmployeeList:IEnumerable{公共委托int CompareDelegate(employee1,Employmee2);私有int容量;//数据成员私有int计数;//数据成员私人雇员[]项;//数据成员public EmployeeList()//默认构造函数{this.capacity=1;this.count=0;this.items=新员工[1];} private EmployeeList(int capacityValue)//初始化构造函数{if(容量值>=1){ this.capacity=容量值;this.count=0;this.items=新员工[capacityValue];}其他的ProcessError(String.Format(“{0}不能是EmployeeList对象的容量\n\nAbort?”,capacityValue);}public EmployeeList(EmployerList sourceList)//复制构造函数{这个。复制(源列表);}public int Count//定义只读Count属性{得到{返回this.count;}}public Employee this[int index]//定义读/写this[]索引器属性{ 得到{  if((索引>=1)&&(索引<=计数)返回this.items[索引-1];其他的{ProcessError(String.Format(“EmployeeList[]Get索引必须介于1和{0}\n\n中止?”,this.count);返回默认值(员工);}}设置{if((索引>=1)&&(索引<=this.count))this.items[索引-1]=值;其他的ProcessError(String.Format(“EmployeeList[]集合索引必须介于1和{0}\n\n中止?”,this.count);}}public bool Empty//如果列表为(非)空,则返回true(false){得到{return(this.count==0);}}public IEnumerator GetEnumerator()//IEnumerable接口实现:{//GetEnumerator()方法的声明,for(int i=1;i<=this.count;i++)//,这是赋予“foreach”含义所必需的收益率返回此[i];//控件构造。} public void Clear()//删除所有列表元素{this.capacity=1;this.count=0;this.items=新员工[1];}public void Copy(EmployeeList sourceList)//这种方法是正确的{这个。清除();foreach(sourceList中的员工)这个。添加(employee.Clone());}公共EmployeeList克隆(){return new EmployeeList(this);}私有void IncreaseCapacity(){EmployeeList tempList;tempList=新员工列表(2*this.capacity);foreach(此处的员工)tempList。添加(员工);this.capacity=tempList.capacity;this.count=临时列表计数;this.items=模板列表.items;}public void Add(员工员工){这个。InsertAt(this.count+1,employee);}public void InsertAt(int位置,员工){整数i;if((位置>=1)&&(位置<=this.count+1)){if(this.count==this.capacity)这个。增加容量();this.count++;对于(i=this.count;i>位置;i--)这个[i]=这个[i-1];this[职位]=员工;}其他的ProcessError(String.Format(“EmployeeList InsertAt索引必须介于1和{0}\n\n中止?”,this.count+1);}公共雇员RemoveAt(int位置){整数i;员工=空;if((位置>=1)&&(位置<=this.count)){employee=此[职位];对于(i=位置;i<此计数;i++)这个[i]=这个[i+1];this.count--;}其他的ProcessError(String.Format(“EmployeeList RemoveAt索引必须介于1和{0}\n\n中止?”,this.count);返聘员工;}private void ProcessError(字符串消息){DialogResult结果;结果=MessageBox。显示(消息,“错误”,MessageBoxButtons.YesNo,MessageBoxIcon.Error,MessageBoxDefaultButton(消息框默认按钮)。按钮1);if(结果==DialogResult.Yes)控制台应用程序。退出();}公共void Reverse(){整数i;员工tempValue;对于(i=1;i<=this.count/2;i++){tempValue=此[i];this[i]=this[this.count+1-i];this[this.count+1-i]=临时值;}}public int Locate(Employee-Employee)//查找并返回列表索引{//列表中的数据。int listIndex=1;while((listIndex<=this.count)&&(this[listIndex].CompareTo(employee)<0))列表索引++;if(listIndex>this.count)listIndex=~listIndex;else if(this[listIndex].CompareTo(employee)>0)listIndex=~listIndex;返回列表索引;}public int Locate(Employee-Employee,CompareDelegate compareMethod)//定位并返回列表索引{//列表中的数据。int listIndex=1;//使用CompareDelegate方法。while((listIndex<=this.count)&&(compareMethod(this[listIndex],employee)<0))列表索引++;if(listIndex>this.count)listIndex=~列表索引;else if(compareMethod(this[listIndex],employee)>0)listIndex=~listIndex;返回列表索引;}public void Sort()//使用Employee类CompareTo方法{整数i,j,k;员工温度;对于(i=1;i<=(this.count-1);i++){k=i;对于(j=(i+1);j<=本计数;j++)if(this[j].CompareTo(this[k])<0)k=j;如果(k>i){温度=此[k];this[k]=this[i];此[i]=温度;}}}public void Sort(CompareDelegate compareMethod)//使用CompareDeligate方法。{整数i,j,k;员工温度;对于(i=1;i<=(this.count-1);i++){k=i;对于(j=(i+1);j<=本计数;j++)if(compareMethod(this[j],this[k])<0)k=j;如果(k>i){温度=此[k];this[k]=this[i];此[i]=温度;}}}public double Total(字符串propertyName){双倍总计=0.0;foreach(此处的员工)total+=双倍。解析(员工[属性名称]);收益总额;}public double Mean(字符串属性名称){返回这个。总计(属性名称)/此。计数;}public double Max(字符串propertyName){整数i;double max=双倍。解析(this[1][属性名称]);对于(i=2;i<=this.Count;i++)if(Double.Parse(this[i][propertyName])>最大值)max=双倍。解析(this[i][propertyName]);返回最大值;}public double Min(字符串propertyName){整数i;double min=双倍。解析(this[1][属性名称]);对于(i=2;i<=this.Count;i++)if(Double.Parse(this[i][propertyName])<分钟)min=双倍。解析(this[i][propertyName]);最小返回值;}public int AssignID(){int returnValue=1;if(this.count>0)returnValue=(int)this。最大值(“ID”)+1;return returnValue;}公共void输入(StreamReader fileIn){字符串lineIn;while((lineIn=fileIn.ReadLine())=null)这个。添加(Employee.Parse(lineIn));}public void PrintReport(StreamWriter fileOut,string orderDescriptor){int indent=(63-orderDescriptor.Length)/2;orderDescriptor=“”。PadLeft(缩进)+orderDescriptor;文件输出。WriteLine(“员工报告”);文件输出。WriteLine(orderDescriptor);文件输出。WriteLine(“{0:MM/dd/yyyy}”,日期:今日);文件输出。WriteLine();文件输出。WriteLine(“Jon Ernst”);if(!this.空){文件输出。WriteLine(“ID姓氏名字雇佣收入FICA联邦税州税净收入”);文件输出。WriteLine(“---------------------------------------------”);foreach(此处的员工)文件输出。WriteLine(“{0:d4}{1,-15}{2,-15}{3,10:MM/dd/yyyy}{4,7:f}{5,7:f{6,7:fneneneep{7,7:f►{8,7:f}”,员工。ID,员工。姓氏,员工。名字,员工。雇佣日期,员工。收入,员工。FICA,员工。联邦税务局,员工。StateTax,员工。NetPay);文件输出。WriteLine(“------------------------”);文件输出。WriteLine(“{0,-52}{1,7:f}{2,7:f}}{3,7:f}{4,7:f{5,7:fneneneep”,“平均值”,this.Mean(“收益”),this。平均值(“FICA”)。意思是(“联邦税”)。意思是(“州税”)。平均值(“净工资”);文件输出。WriteLine(“{0,-52}{1,7:f}{2,7:f}{3,7:f}{4,7:f}{5,7:f}”,“最大值”,this。最大值(“收益”),this。Max(“FICA”),这个。Max(“联邦税”),这是。Max(“州税”),这个。最高工资(“净工资”);文件输出。WriteLine(“{0,-52}{1,7:f}{2,7:f}}{3,7:f}{4,7:f{5,7:fneneneep”,“最小值”,this.Min(“收益”),this。Min(“FICA”),这个。Min(“联邦税收”),这是。Min(“国税”),这是。Min(“净工资”);文件输出。WriteLine();文件输出。WriteLine(“计数={0}”,this.Count);}其他的文件输出。WriteLine(“列表为空。”);文件输出。WriteLine();}}//结束类EmployeeList}//结束命名空间LibEmployee1

Visual Studio指出此方法中的堆栈溢出错误:

public Date HireDate//定义读/写HireDateproperty{得到{**<===visual studio将溢出错误专门指向这一行**返回这个。雇佣日期;}设置{这个。雇佣日期。副本(值);}}
8
  • 这是一个程序的一部分,该程序应该在文本文件中提供工资单报告 2014年6月27日20:12
  • 您的返回是调用属性本身的getter。这会导致无限递归,导致堆栈溢出。 2014年6月27日20:16
  • 1
    HireDate属性getter返回HireDateproperty的值。其属性getter返回HireDate属性的值。其属性getter返回HireDate属性的值。谁的道具。。。。卡布ooom! 2014年6月27日20:16
  • 在默认构造函数中,您正在设置私有属性(好的),而在其他构造函数中,则直接设置公共属性(坏的),这可能无法解决您当前的问题。。。只是指出:) 2014年6月27日20:16
  • 2
    为了帮助自己注意到这类东西,请使用_,例如私有int _id;-这样,你或其他任何人都会把他们弄糊涂;这也是通常的惯例。 2014年6月27日20:17

2个答案2

重置为默认值
1

区分大小写问题。您正在递归调用属性getter。返回this.hireDate此外,无需复制setter中的日期。它是一种值类型。

0

答案就在你的代码里,在你的雇用日期属性,则返回雇用日期属性,该属性将返回雇用日期属性等。。!您可能想返回支持字段的值。

公共日期时间租用日期{得到{返回this.hireDate;}设置{this.hireDate=值;}}

您的答案

单击“发布您的答案”,表示您同意我们的服务条款并确认您已阅读我们的隐私政策.

不是你想要的答案吗?浏览标记的其他问题问你自己的问题.