user1885308
user1885308

Reputation: 1

An unhandled exception of type 'System.StackOverflowException'

I have created a dll with controls. when i browse the dll it adds the controls to the toolbox successfully. the problem is that when i run the application, i get the following error : An unhandled exception of type 'System.StackOverflowException' occurred in xxx.dll

the method where the debugger goes highlight the error is in the function below:

public ItemType this[int i]
{
    get
{
    return (ItemType)this[i];
}
set
{
    this[i] = value;
}
}

as i know this error occurs because of recursive call, how can i rewrite the above or modify it to overcome this issue. Please any code help as soon as possible

Thanks

Upvotes: 0

Views: 378

Answers (2)

mbday816
mbday816

Reputation: 1

I solved the problem, as follows:

public ItemType this[int i] 
{ 
    get 
    { 
        return (ItemType)((IList)this)[i]; 
    } 
    set 
    { 
        this[i] = value; 
    } 
}

Upvotes: 0

Tobias
Tobias

Reputation: 2985

You should use an internal list in your Class.

    private IList<ItemType> _list = new List<ItemType>();
    public ItemType this[int i]
    {
        get
        {
            return _list[i];
        }
        set
        {
            _list[i] = value;
        }
    }

Upvotes: 2

Related Questions