asp.net c#类属性stackoverflow异常(asp.net c# class properties stackoverflow exception)

编程入门 行业动态 更新时间:2024-10-23 07:38:51
asp.net c#类属性stackoverflow异常(asp.net c# class properties stackoverflow exception)

我有一个非常简单的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

本文发布于:2023-07-18 04:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154903.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   类属   asp   net   stackoverflow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!