- Home /
Hey guys, the following code gives me a StackOverFlowException and I'm just wondering how i can fix it. Thanks :)
To be more specific, the weight section, highlighted in bold.
public class ItemBaseStats : ItemBase {
private int weight;
private int value;
public int Weight
{
get { return Weight; }
**set { Weight = value; }**
}
public int Value
{
get { return Value; }
set { Value = value; }
}
}
I am not sure why it is throwing a StackOverflowException. However, change the private variable that is declared as "value" for sure. That is definitely contributing to the problem. The contextual keyword value is used in the set accessor in ordinary property declarations -as you are using for Weight and Value. However, having an actual private property named value will cause the contextual keyword in the set accessor to behave with adverse side-effects. namely, it may cause the exception.
https://msdn.microsoft.com/en-us/library/a1khb4f8.aspx
Change the private properties to _value and _weight. That is a better convention to use for private, class-level properties in C# anyway.
[1]: https://msdn.microsoft.com/en-us/library/a1khb4f8.aspx
Ah yeah thanks, i changed it to worth, thanks for pointing that out :)
Answer by NoseKills · Apr 15, 2017 at 10:09 AM
You have integer fields weight
and value
with lower case letters that you are not using at all and then you have Weight
and Value
with capital letters that are the names of your properties.
Your problem is that both your properties access themselves. You should be getting an endless recursion and an overflow exception in both the getter and the setter of both your properties.
public int Weight {
// getting the value of Weight tries to read from Weight which
// will try to read it from Weight which will try to read it...
get { return Weight; }
// setting the value of Weight tries to assign to Weight which
// will try to assign to Weight which will try to assign it...
set { Weight = value; }
}
You have to either use the 2 int fields to "back up" the data that these properties read from and assign to
public int Weight
{
get { return weight; }
set { weight = value; }
}
public int Value
{
get { return value; }
// can't test/not sure this will work. You might need to rename 'int value'
set { this.value = value; }
}
Or just use automatically backed up properties
public int Weight
{
get;
set;
}
public int Value
{
get;
private set; // getter and setter can even have different access modifiers
}
How did I miss that at least I was right about what stack overflow was lol
Hey thanks, all i had to do was change the weight and value to lower case and it worked :)
Answer by awinebrenner · Apr 15, 2017 at 07:57 AM
It shouldn't give you that error, at least not just that code. I'm not positive but I think a stack overflow is basically when a script has too much recursion or infinite recursion. You should check any scripts that set the weight and see if they are setting it multiple times or if something recalls the set script. In mono develop you should be able to right click Weight and click references. That will show you every script that calls it.
Your answer
Follow this Question
Related Questions
BurgZergArcade RPG - BaseCharacter script errors. 3 Answers
WHY SAYS ME THAT IS AN ERROR? 1 Answer
I'm getting a StackOverFlowException 1 Answer
Error CS1503, Error CS1502 1 Answer
Targetting Script Error Help 1 Answer