title: Dictionary properties are always null despite dictionaries being instantiated
body: <p>To begin with, I have two projects under the names of <code>Components</code> and <code>WindowsFormsApplication5</code> as part of my solution (there are more, but those are irrelevant). The dictionaries I am referring to are in <code>Components</code>, defined as follows, along-with their properties -:</p>
  
  <pre><code> Dictionary&lt;int, ButtonBase&gt; RespMCQ, DispMCQ; //&lt;Question Number, Checkbox/Radiobutton&gt;
   Dictionary&lt;int, List&lt;ButtonBase&gt;&gt; RespMSQ, DispMSQ;
   public Dictionary&lt;int, ButtonBase&gt; MCQResp
   {
       get
       {
           return RespMCQ;
       }
       set
       {
           RespMCQ = value;
       }
   }
   public Dictionary&lt;int, List&lt;ButtonBase&gt;&gt; MSQResponse
   {
       get
       {
           return RespMSQ;
       }
       set
       {
           RespMSQ = value;
       }
   }
   public Dictionary&lt;int, ButtonBase&gt; MCQDisplay
   {
       get
       {
           return DispMCQ;
       }
       set
       {
           DispMCQ = value;
       }
   }
   public Dictionary&lt;int, List&lt;ButtonBase&gt;&gt; MSQDisplay
   {
       get
       {
           return DispMSQ;
       }
       set
       {
           DispMSQ = value;
       }
   }
  </code></pre>
  
  <p>The dictionaries are instantiated in the constructor of <code>QuestionDisplay.cs</code>, which is part of the <code>Components</code> project (this is where they reside). This is shown as follows -:</p>
  
  <pre><code>public QuestionDisplay()
  {
      InitializeComponent();
      RespMCQ = new Dictionary&lt;int, ButtonBase&gt;();
      DispMCQ = new Dictionary&lt;int, ButtonBase&gt;();
      RespMSQ = new Dictionary&lt;int, List&lt;ButtonBase&gt;&gt;();
      DispMSQ = new Dictionary&lt;int, List&lt;ButtonBase&gt;&gt;();
      ....
  }
  </code></pre>
  
  <p>In <code>Form1.cs</code> of <code>WindowsFormsApplication5</code> (I did not choose this name, pardon me if it sounds bland), I am using the properties I mentioned. There are no namespace issues and compilation occurs as normal.</p>
  
  <p>During the <code>Form_Load</code> of <code>Form1</code>, however, when I refer to these properties, they throw a <code>NullReferenceException</code> saying -:</p>
  
  <pre><code>Object reference not set to an instance of an object.
  </code></pre>
  
  <p>The first occurrence of these properties is in a method called <code>WorkFlowPanel(string S, QuestionContainerState obj)</code> which is called at the time of form load -:</p>
  
  <pre><code> if (QuesTypes[i].Equals("MCQ"))
   {
      string text = "";
      for (int ansCount = 0; ansCount &lt; questions[i].Options.Count(); ansCount++)
      {
          if (Convert.ToInt32(K.Key).Equals(ansCount + 1))
          {
              text = questions[i].Options[ansCount];
              break;
          }
      }
      questionDisplay1.MCQResp.Add(i, new CustomRadio { optionId = Convert.ToInt32(K.Key), Checked = true, Text = text }); //Using anonymous class
   }
   else if (QuesTypes[i].Equals("MSQ") || QuesTypes[i].Equals("Linked"))
   {
       var storeoptions = K.Key.Split(':');
       questionDisplay1.MSQResponse.Add(i, new List&lt;ButtonBase&gt;());
       for (int s = 0; s &lt; storeoptions.Count(); s++)
       {
           questionDisplay1.MSQResponse[i].Add(new CustomChecks { optionId = Convert.ToInt32(storeoptions[s]), Checked = true, Text = questions[i].Options[Convert.ToInt32(storeoptions[s])] });
       }
   }
  </code></pre>
  
  <p>To my surprise, on debugging, I found that the properties are always null despite the dictionaries being instantiated. Now this problem has a bit of a history which I will have to delve into, to provide context.</p>
  
  <p>I was earlier battling with a <code>Could not find a type for a name. The type was System.Collections.Dictionary...</code> bug which was occurring in <code>Form1.resx</code> (I cannot recall the entire error). Apparently the dictionary properties were being <strong>serialized</strong> into binary format and encoded as base64 in the resx file, as follows -:</p>
  
  <pre><code>&lt;data name="MCQDisplay" mimetype="application/x-microsoft.net.object.binary.base64"&gt;
  &lt;value&gt;
          AAEAAAD/////AQAAAAAAAAAEAQAA....
  &lt;/value&gt;
  &lt;data name="MSQDisplay" .....&gt;
  ....
  ....
  </code></pre>
  
  <p>I followed a solution I found on Stackoverflow, which told me to copy the code back into an older version. This is the fix I had been applying since then. I found that if I removed these entries (which the compiler error asked me to do), the dictionary properties would always be null. I feel this is tied to my current scenario. It so happened that during the copy-back operation, the resx file entries somehow vanished. </p>
  
  <p>Since then, it has never thrown this compiler error. I tried everything, including the use of <code>ResXResourceWriter</code> to regenerate the entries. It's like they're not even acknowledged, and instead of the error, I'm told during run-time that the dictionary properties are always null! One person on IRC suggested that the error used to occur because <code>ButtonBase</code> is not serializable, even though <code>Dictionary</code> in itself is. Well, I never asked the compiler to serialize the dictionaries. I wonder why that even happened.</p>
  
  <p>I would be really grateful if anyone could help me resolve this damning bug. I also had this vague solution in my head that instead of storing <code>ButtonBase</code> children as values, I could store class objects instead which have properties mirroring the text, optionId and checked properties of the <code>ButtonBase</code> child. However, I am thinking of this as a last resort.</p>
  
  <p><strong>EDIT_1</strong>: I don't know if this is relevant, but I found these entries in <code>Form1.Designer.cs</code> -:</p>
  
  <pre><code>this.questionDisplay1.MCQDisplay = ((System.Collections.Generic.Dictionary&lt;int, System.Windows.Forms.ButtonBase&gt;)(resources.GetObject("questionDisplay1.MCQDisplay")));
  this.questionDisplay1.MCQResp = ((System.Collections.Generic.Dictionary&lt;int, System.Windows.Forms.ButtonBase&gt;)(resources.GetObject("questionDisplay1.MCQResp")));
  this.questionDisplay1.MSQDisplay = ((System.Collections.Generic.Dictionary&lt;int, System.Collections.Generic.List&lt;System.Windows.Forms.ButtonBase&gt;&gt;)(resources.GetObject("questionDisplay1.MSQDisplay")));
  this.questionDisplay1.MSQResponse = ((System.Collections.Generic.Dictionary&lt;int, System.Collections.Generic.List&lt;System.Windows.Forms.ButtonBase&gt;&gt;)(resources.GetObject("questionDisplay1.MSQResponse")));
  </code></pre>
  
  <p>Do I have to remove these or modify them in some way? I feel these are somehow related to Matthew's answer.</p>
  
  <p><strong>EDIT_2</strong>: I managed to solve this problem by applying the following -:</p>
  
  <ol>
  <li>Matthew Watson's non-serialization fix - incase the compiler tried to serialize the dictionary properties again.</li>
  <li>I commented out the entries in the <code>Form1.Designer.cs</code>, the ones that I mentioned above.</li>
  </ol>
  
  <p>I am accepting Matthew's answer because I feel the non-serialization fix is relevant.</p>