Custom setter but autograder

I have an object with a property for which I want to create a custom setter and also keep the automatic getter:

public class SomeObject { public int SomeProp { get; set; } public Nullable<short> MyProp { get { return MyProp; } set { if (value != null) { SomeProp = SomeWork(value); MyProp = value; } } } } 

The problem is that I get a Stackoverflow error message on the receiver. How to implement a property in which I store the getter as is, but only change the installer?

+6
source share
4 answers

You need to use the support box for your property. You get a SO error because you recursively refer to your MyProp property in your own getter, which leads to infinite recursion.

 private short? _myProp; public short? MyProp { get { return _myProp; } set { if (value != null) { SomeProp = SomeWork(value); _myProp = value; } } } 
+7
source

You get an exception because you essentially do infinite recursion in your property.

Unfortunately, what you describe is not yet possible in C #, you need to define the support field yourself.

+3
source

No, It is Immpossible. Or you make it the full auto (OR) property, which defines both getter and setter , in which case you will need to provide a support field, since the compiler will not create it for you.

+2
source

You cannot do this. You either use custom getter setters (both non-single), or use autograders.

In your code, the getter of your property tries to access the getter by itself (which will have access to the same getter, etc. etc.)

So, this is an endless cycle that is going to destroy the whole world and put an end to humanity

+1
source

All Articles