Stack Overflow Exception in C# Setter

Stack overflow exception in C# setter

When you write a = value, you are calling the property setter again.

In order to use non-automatic properties, you need to create a separate private backing field, like this:

ConstraintSet a;
public ConstraintSet A { get { return a; } set { a = value; } }

StackOverflowException in Variable SET function

You should not use properties getters/setters inside their own getters or setters respectively, that is what basically causes the stackoverflow. In this code:

public int X { get => X; set => X = value; } \\ <--- StackOverflow on X.set 

When you try to set X property internally it will recursively call it's own setter, which in turn will call it again ... and so on until you run out of stack space.

One way to fix it - just change all this definitions to just ordinary auto-implemented properties:

public int X { get; set; } 

StackOverFlowException when setting property

The problem is you re-assigning within the same Property which causes recursion:

       public string Name
{
get { return Name; }
set
{
if (Name.CompareTo("Admin") == 0 || Name.CompareTo("Admin") == -1) //Just trying out comparison with the input.
{
Console.WriteLine("Invalid Name."); //To see if an invalid input that is not "Admin" fails.
}
else
{
Name = value; //StackOverFlow here
Console.WriteLine("Done.");
}
}
}

If you are validating it, you might wanna try a different approach.

Solution #1 (Using separate variable for Name property)

private string name = "";
public string Name
{
get { return name; }
set
{
if (value != null) //Check for null before validation (or it's up to you how to handle NULL value)
{
if (value.CompareTo("Admin") == 0 || value.CompareTo("Admin") == -1) //Just trying out comparison with the input.
{
Console.WriteLine("Invalid Name."); //To see if an invalid input that is not "Admin" fails.
}
else
{
name = value;
Console.WriteLine("Done.");
}
}
}
}

Solution #2 (Making set private and using separate setter method.

public string Name
{
get;
private set; //Making this private means you can still
//access this setter within the class via `this.Name =` (so be careful)
}

/// <summary>
/// Defined a public setter which can be invoked outside
/// </summary>
/// <param name="value"></param>
public void SetName(string value)
{
if (value != null) //Check for null before validation (or it's up to you how to handle NULL value)
{
if (value.CompareTo("Admin") == 0 || value.CompareTo("Admin") == -1) //Just trying out comparison with the input.
{
Console.WriteLine("Invalid Name."); //To see if an invalid input that is not "Admin" fails.
}
else
{
Name = value;
Console.WriteLine("Done.");
}
}
}

public Workers(string Name)
{
this.SetName(Name); //be careful making this.Name = Name or else no validation
}

public Workers()
{
this.SetName(null); //be careful with this.Name = null or else no validation
}

The disadvantage of the second approach is that inside the class Workers you should be careful when setting this.Name because it still accessible inside Workers even when set to private.

StackOverflowException in property setter

Your Metres property has the same name as the backing field (variable), and is being recursively called. You may have been confused by automatic properties versus declaring the 'backing' field yourself.

You can implement like this:

class Distance 
{
private double _meters;

public double Meters
{
get
{
return _meters;
}
set
{
_metres = value;
}
}

.....

Or like this:

class Distance 
{
public double Metres{ get; set; }

....

Ref: Auto-Implemented Properties

Stack Overflow Exception While Using Session Inside Getter Setter

The reason is in this line

get { return (List<Flight>)(Session["Flight"] = flightsInCart); }

Your property is named flightsInCart. So from inside your getter, you call the getter again. This leads to the StackOverflowException as you create an infinite recursion.

The same goes for your setter.

You probably wanted to create a backing field and name the property with a capital letter:

// backing field
private List<Flight> flightsInCart;

// property
private List<Flight> FlightsInCart // <- with capital (c# naming convention)
{
get { return (List<Flight>)(Session["Flight"] = flightsInCart); }
set { flightsInCart = value/*(List<Flight>)Session["Flight"]*/; }
}

Stackoverflow error C# with getter and setter

This is an endless loop:

public string Name { get {
...
set
{
Name = value;
}
}

The setter will call itself repeatedly until you get the Stack overflow exception.

usually you have a backing variable, so it ends up like this

private string name;
public string Name {
get {
if (null != FriendlyName)
return FriendlyName;
else
return name;
}
set {
name = value;
}
}

StackOverflow Exception from get and set

public DateTime time
{
get { return this.time; }
set
{
this.time = value;
}
}

you aren't using backing fields, but setting the property itself from within the property setter.

You can fix this by using 1) an auto property

public DateTime Time { get; set; }

or 2) a backing field

private DateTime _time;
public Datetime Time
{
get { return _time; }
set { _time = value; }
}

they both equate to the same code.

For an explanation, when you get time in your code:

get { return this.time; } 

it has to retrieve the value of time to return. It does that by calling the get on time, which has to get retrieve the value of time, etc.

Overloading getter and setter causes a stack overflow in C#

Yes, you do not have a backing field... this is how you should do it:

private MyEnumType data;

public MyEnumType Data
{
get
{
return data;
}
set
{
data = value;
}
}

What happens is that you are referring to the property to return itself, which causes an infinite loop of trying to access its own value. Hence, a stack overflow.

In your case when you do not add any additional logic in the get and set methods you could use an automatic property as well. This is simply defined like so:

public MyEnumType Data
{
get;
set;
}

System.StackOverflowException' on setter

Your setter calls itself recursively. If the value of Name is calculated from the other properties, you should probably omit the setter entirely. Since it's a calculated entirely from other properties, you probably don't need it to be serialized either.

If having a setter is an absolute must, you can just create an empty setter like this:

public string Name  {
get { return string.Format("{0} {1}", FirstName, LastName); }
internal set { }
}

Why does Property Set throw StackOverflow exception?

It's because you're recursively calling the property - in the set you are setting the property again, which continues ad infinitum until you blow the stack.

You need a private backing field to hold the value, e.g.

private string firstName;

public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
}
}

Alternatively, if you're using C# 3.0, you could use an auto-property, which creates a hidden backing field for you, e.g.

public string FirstName { get; set; }


Related Topics



Leave a reply



Submit