Reputation: 139
I am getting an error like
"An unhandled exception of type 'System.StackOverflowException' occurred in ciscontrols.dll".
My code is given below
private int _vin;
public int MaxLength
{
get { return _vin; }
set //Here your answer solve the promblem
{
txtLocl.MaxLength = value;
if (value < 2)
{
throw new ArgumentOutOfRangeException("MaxLength","MaxLength MinValue should be 2.");
}
else this._vin = value;
}
}
i am creating a new property for decimal places
private int Dval;
public int DecPlaces
{
get { return Dval; }
set// here it showing the same error
{
DecPlaces = value; // MaxLength is a preDefined Property but DecPlaces created by me.
if (value < 2)
{
throw new ArgumentOutOfRangeException("decplaces", "decimal places minimum value should be 2.");
}
else this.Dval = value;
}
}
Upvotes: 0
Views: 2247
Reputation: 2759
your property's setter is calling itself on this line:
this.MaxLength = value;
Change it to:
set //Here i am getting Error
{
txtLocl.MaxLength = value;
if (value < 2)
{
throw new ArgumentOutOfRangeException("MaxLength","MaxLength MinValue should be 2.");
}
else this._vin = value;
}
For this particular exception, the Call Stack window displayed in Visual Studio while debugging is helpful in seeing which methods are calling each other in an infinite loop. The setter of a property eventually becomes a method called set_MaxLength in the run-time.
Upvotes: 2
Reputation: 13601
this.MaxLength = value
This line triggers an infinite loop, because it calls the set
accessor that you are already in. Did you mean to set the value of a backing variable instead?
Upvotes: 1
Reputation: 149000
Your setter is recursive because you have
this.MaxLength = value;
Within your setter. This will result in an infinite loop, and eventually, a StackOverflowException
.
Use
this._vin = value;
Instead
Upvotes: 2