This is my property on my code :
public KPage Padre
{
get
{
if (k_oPagina.father != null)
{
this.Padre = new KPage((int)k_oPagina.father);
}
else
{
this.Padre = null;
}
return this.Padre;
}
set { }
}
but it says :
An unhandled exception of type ‘System.StackOverflowException’ occurred in App_Code.rhj3qeaw.dll
Why? How can I fix it?
EDIT
After correct the code, this is my actual code :
private KPage PadreInterno;
public KPage Padre
{
get
{
if (PadreInterno == null)
{
if (paginaDB.father != null)
{
PadreInterno = new KPage((int)paginaDB.father);
}
else
{
PadreInterno= null;
}
}
return PadreInterno;
}
}
What do you think about?
The property is calling itself… usually properties call underlying fields:
Your old code was recursively calling the
getof thePadreproperty, hence the exception.If your code only “gets” and doesn’t need to store the value, you could also get rid of the backing field entirely:
That said, I’d put this in a method.
This is also the same issue as the one you asked a couple of days ago:
An unhandled exception of type 'System.StackOverflowException' occurred