开发者

asp.net c# class properties stackoverflow exception

开发者 https://www.devze.com 2023-04-06 18:41 出处:网络
I have a very simple C# class: namespace mybox { public class userAccount : IMyInterface { public userAccount()

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?


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; }         
} 


It is because userName calls itself. You probably meant to assign the field:

This line is wrong:

set { userName = value; }

You meant to write:

set { _userName = value; }


You need to set the private backing field, not the property. Otherwise you are just going into infinite recursion as you call set on yourself the entire time.

    private int _userId;
    private string _userName;

    public int userId
    {
        get { return _userId; }
        set { _userId = value; }
    }
    public string userName
    {
        get { return _userName; }
        set { _userName = value; }
    }

In your case, you could just use auto implemented properties (I changed the casing to match the guidelines):

public int UserId { get; set; }
public string UserName { get; set; }


If you re-write your setters a bit, it's easy to see what's happening. The get and set on a property are actually compiled down to methods (PropType get_PropName() and void set_PropName(PropType value)), and any references to the property are compiled to calls to the appropriate method. So this code:

int i = myObj.MyIntProp;
myObj.MyIntProp = 6;

compiles to

int i = myObj.get_MyIntProp();
myObj.set_MyIntProp(6);

So your setter

set
{
    username = value;
}

actually compiles to

public void set_username(string value)
{
    set_username(value);
}

And now the cause of the stack overflow is obvious.

0

精彩评论

暂无评论...
验证码 换一张
取 消