琐碎的错误通常最难找到!多次这样做,但不知道为什么抛出错误
我需要在属性FullName.So中将员工名字和姓氏连接起来,组合框的显示成员可以设置为" FullName"。
为此我只创建了另一个与我的数据模型中生成的Employee类相对应的部分类,如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
namespace IGarage.DAL
{
public partial class Employee : EntityObject
{
public string FullName
{
get
{
return FullName;
}
set
{
value = this.FirstName + " " + this.LastName;
}
}
}
}
在名为Employee.cs的文件中
但是当我查询与Employee相关的数据库时,它会抛出以下错误:
当我探索这个问题时,我也看到了这个
请建议。
答案 0 :(得分:4)
如评论中所述,您的实现不起作用,因为FullName属性的getter再次调用自身,这会导致无限递归。此外,修改setter中的值变量并不能做你想要的。它不会将字符串存储在任何地方(除了在setter末尾超出范围的局部变量)。
据我所知,你想要一个完全没有setter的只读属性:
public string FullName
{
get
{
return this.FirstName + " " + this.LastName;
}
}
答案 1 :(得分:0)
public string FullName
{
get
{
return FullName;
}
}
这里你调用属性的getter,你正在实现哪个getter,这会导致无限递归。 顺便说一句。问问自己你是否真的需要这个属性的setter,因为FullName是一个名字和姓氏的连接。就个人而言,我会这样做:
public string FullName
{
get
{
this.FirstName + " " + this.LastName;
}
}