Search code examples
c#asp.netautomatic-properties

Assigning a value to baseclass property and accessing it from child class causing the debugger to Force stop


i have a Base class for properties

  public class Parameters
  {
    public string caption
    {
        get;
        set;
    }

    public string subcaption
    {
        get;
        set;
    }
    public string bgColor
    {
        get
        {
            if (bgColor == " ")
                return bgColor = "FFFFFF";
            else
                return bgColor;
        }
        set { bgColor = value; }
    }
 }
 //some other properties with default return values

and other class inherits from this class and has a method to return stringbuilder

    public class Bar : Parameters
      {
    public StringBuilder GetXML()
    {
        StringBuilder xmlData = new StringBuilder();
        xmlData.Append("<chart bgColor='" + bgColor + "'"
                     + " caption='" + caption + "'"
                     + " subcaption='" + subcaption + "'>");//mentioned here are some properties there are many in the xmldata.append function 
        return xmlData;
    }
      }

now some other class tries to access this method by declaring object

      Bar XML = new Bar();
            XML.caption = "Caption";
            XML.subcaption = "subcap"; //setting values properties which dont have default return values 
            XML.GetXML();

when the debugger enters GetXML method debugger force closes himself, is there any thing i am missing here is it something related to this


Solution

  • This is just broken code.

     return bgColor = "FFFFFF";
    

    Will evaluate to boolean and not string.

    You are also trying to use a non-existing backing field in your getter.

    Try something like this:

    private string _bgColor;
      public string bgColor
      {
         get
         {
            if (string.IsNullOrEmpty(_bgColor))
            {
               _bgColor = "FFFFFF";
            }
    
            return _bgColor;
         }
         set { _bgColor = value; }
      }