C # Error - But How To Fix It?

I came across a really interesting runtime error that generates an overflow of a rogue stack.

I defined the structure as follows:

public enum EnumDataType { Raspberry, Orange, Pear, Apple }; public class DataRequest { public long DataSize { get { return 0; } set { DataSize = value; } } public EnumDataType DataType { get { return EnumDataType.Apple; } set { DataType = value; } } } 

The following lines work fine:

 DataRequest request = new DataRequest(); request.DataSize = 60; 

However, when I go to the next line in the code, it generates a stack overflow:

 request.DataType = EnumDataType.Raspberry; 

Of course, I can fix this by deleting the default values ​​or using auto get / set, but I need it to be readable and writable and return the default value - any ideas?

+4
source share
5 answers

As others have said, a stack overflow occurs because your property installer just calls itself. It might be easier to understand if you think of it as a method:

 // This obviously recurses until it blows up public void SetDataType(long value) { SetDataType(value); } 

As I understand it, you are trying to create normal properties, but with a default value, right?

In this case, you need the backup variables that are set by the setters, and the recipients must also return these variables. These are the variables that should receive the default values:

 private long dataSize = 0; public long DataSize { get { return dataSize; } set { dataSize = value; } } private EnumDataType dataType = EnumDataType.Apple; public EnumDataType DataType { get { return dataType; } set { dataType = value; } } 

Alternatively, use automatic properties, but set the default values ​​in your constructor:

 public long DataSize { get; set; } public EnumDataType DataType { get; set; } public DataRequest() { DataSize = 0; // Not really required; default anyway DataType = EnumDataType.Apple; } 
+12
source
 public long DataSize { get { return 0; } set { DataSize = value; } } 

You are constantly setting DataSize . You need to create a local variable and use it instead. eg.

 private long dataSize; public long DataSize { get { return this.dataSize; } set { this.dataSize = value; } } 

EDIT I wrote DataSize , but the same thing applies to DataType

+18
source

stack overflow occurs because in the customizer you set the property to (i.e., you are trying to get something to set something ... which causes an infinite loop) ... which means that it is trying to set itself to value, which means that he is trying to set the value to boom

your properties will never get the values ​​you set, because they always return the same value (and not the stored value)

 public enum EnumDataType { Raspberry, Orange, Pear, Apple }; public class DataRequest { private long _dataSize = 0; private EnumDataType _dataType = EnumDataType.Apple; public long DataSize { get { return _dataSize ; } set { _dataSixe= value; } } public EnumDataType DataType { get { return _dataType; } set { _dataType= value; } } } 

- this is what you really wanted

+4
source

you need to execute it using the backup storage:

 private EnumDataType dataType; public EnumDataType DataType { get { return EnumDataType.Apple; } set { dataType = value; } } 

}

You should do this at any time when you are doing some kind of ICQ in getters and setters. By the way, why can you set variables? you cannot read them, you always get EnumDataType.Apple. If you want an initial value, you can do the following:

 private EnumDataType dataType = EnumDataType.Apple; public EnumDataType { get { return dataType; } set { dataType = value; } } 
+3
source

I do not understand how the first line is: request.DataSize = 60;

Doesn't cause a stack overflow - my advice would be to use backup properties:

 public class DataRequest { protected int dataSize = 0; protected EnumDataType enumDataType; public long DataSize { get { return 0; } set { dataSize = value; } } public EnumDataType DataType { get { return EnumDataType.Apple; } set { enumDataType = value;} } 
+2
source

All Articles