我有一个非常简单的C#类:
namespace mybox { public class userAccount : IMyInterface { public userAccount() { } private int _userId; private string _userName; public int userId { get { return _userId; } set { userId = value; } } public string userName { get { return _userName; } set { userName = value; } } public string list(int myUserId) { ... myOutPut = string.Format("{0} {1} {2}", u.userId, u.userName); return myOutPut.ToString(); } public void add() { pillboxDataContext db = new pillboxDataContext(); userAccount newUser = new userAccount(); newUser.userName = "test123"; db.SubmitChanges(); } } }在我的Page_Load事件中的default.aspx.cs中,我正在尝试调用list方法:
protected void Page_Load(object sender, EventArgs e) { pillbox.userAccount myUA = new pillbox.userAccount(); myUA.add();// Console.WriteLine(myUA.list(1));
}当我调用add方法时,我可以看到它正在尝试将值test123分配给属性,但是我得到以下消息:
App_Code.1zm0trtk.dll中发生未处理的“System.StackOverflowException”类型异常
我正在做错的任何想法?
I have a very simple C# class:
namespace mybox { public class userAccount : IMyInterface { public userAccount() { } private int _userId; private string _userName; public int userId { get { return _userId; } set { userId = value; } } public string userName { get { return _userName; } set { userName = value; } } public string list(int myUserId) { ... myOutPut = string.Format("{0} {1} {2}", u.userId, u.userName); return myOutPut.ToString(); } public void add() { pillboxDataContext db = new pillboxDataContext(); userAccount newUser = new userAccount(); newUser.userName = "test123"; db.SubmitChanges(); } } }In my default.aspx.cs in the Page_Load event I'm trying to call the list method:
protected void Page_Load(object sender, EventArgs e) { pillbox.userAccount myUA = new pillbox.userAccount(); myUA.add();// Console.WriteLine(myUA.list(1));
}When I call the add method I can see that it is trying to assign the value test123 to the property but I get the following message:
An unhandled exception of type 'System.StackOverflowException' occurred in App_Code.1zm0trtk.dll
Any ideas of what I'm doing incorrectly?
最满意答案
问题在于您定义属性的方式。
您正在尝试引用要在setter中为其指定值的属性,该属性将导致infinte递归(具体而言,此行触发它newUser.userName = "test123"; )。
将它们更改为:
public int userId { get { return _userId; } set { _userId = value; } } public string userName { get { return _userName; } set { _userName = value; } }The problem is with the way you defined your properties.
You are trying to refer to the property to which you are assigning the value in the setter which is resulting in an infinte recursion (to be specific this line is triggering it newUser.userName = "test123";).
Change them to:
public int userId { get { return _userId; } set { _userId = value; } } public string userName { get { return _userName; } set { _userName = value; } }更多推荐
public,test,list,add,电脑培训,计算机培训,IT培训"/> <meta name="descript
发布评论