system.stackoverflowexception when passing an array in the constructor

maurizio verdirame 41 Reputation points
2021-09-09T13:31:53.86+00:00

I'm trying to pass to the constructor a string array containing two parameter. the code is:

string[] Parametri = new string[2];
Parametri[0] = this.txtName.Tag.ToString();
Parametri[1] = null;
dativisitafrm.Parametri = Parametri;
dativisitafrm.ShowDialog();

the constructor code is this:

***public string[] Parametri
{

        get { return  Parametri; }

}***

I get this error: System.StackOverflowException: 'Exception of type 'System.StackOverflowException' is thrown.' can someone help me please

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,626 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Adam Jachocki 46 Reputation points
    2021-09-15T09:41:17.737+00:00

    Your problem is here:

    public string[] Parametri
    {
        get {return Parametri;}
    }
    

    Notice that you are returning the property itself. Then the getter of the property is running. Getter returns the property itself. Then the getter of the property is running. Getter returns the property itself. Then the..... And stack overflow.

    Just don't return the property itself in the getter. I think that you wanted to do something like this:

    public string[] Parametri {get; private set;}
    

    or like this:

    string[] parametri;
    public string[] Parametri
    {
        get { return parametri; }
    }
    

    Notice that I don't return here the property itself, just the value connected with the property.

    0 comments No comments