Search code examples
c#mef

Stack overflow when setting variable


I am writing a program using the MEF framework to create plugins. When trying to set a variable in one of the plugins, I am getting a stack overflow exception.

the variable in the plugin is defined as public string bnick {get {return bnick;} set {bnick = value;}}

the calling code in the main program:

 public void SetUpPlugins()
        {
        foreach (Plugin p in plugins)
            {
            p.bnick = nick;
            p.HostProgram = this;
            }
        }

Using the debugger I determined that the line p.bnick = nick is only getting called once. And it never gets to the next line.

Why is this filling up the stack and how do I fix it?


Solution

  • public string bnick {get {return bnick;} set {bnick = value;}} 
    

    Here, you are assigning bnick in the body of the setter again, creating a stack overflow. Did you intend to create an instance Variable instead, something like

    private string bnick = "";
    public string Bnick 
    {
      get 
      { 
       return bnick;
      } 
      set 
      { 
        bnick = value;
      }
    } 
    

    Note: in C#, the convention is to write properties in PascalCase.