Search code examples
c#.netarraysapplication-settings

Problems while adding items to an array in the settings.settings


I am currently in the process of creating an application where the user will be able to create announcements. I would like to save the users announcements and have tried everything to save the user input in the form of a setting. Any suggestions/different methods for saving user input would be appreciated.

Current Setting's

My attempt at adding new "Announcements":

private void button1_Click(object sender, EventArgs e)
{
    Properties.Settings.Default.Title[Properties.Settings.Default.MessageNum] = textBox1.Text;
    Properties.Settings.Default.Message[Properties.Settings.Default.MessageNum] = textBox2.Text;
    Properties.Settings.Default.MessageNum++;
    Properties.Settings.Default.Save();
}

Error thrown

{"Object reference not set to an instance of an object."}

settings.settings xml file as requested

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WindowsFormsApplication1.Properties" GeneratedClassName="Settings">
  <Profiles />
  <Settings>
    <Setting Name="Title" Type="System.String[]" Scope="User">
      <Value Profile="(Default)" />
    </Setting>
    <Setting Name="Message" Type="System.String[]" Scope="User">
      <Value Profile="(Default)" />
    </Setting>
    <Setting Name="MessageNum" Type="System.Int32" Scope="User">
      <Value Profile="(Default)">0</Value>
    </Setting>
  </Settings>
</SettingsFile>

Solution

  • Use a StringCollection instead of string[]. StringCollection is supported by VS.

    enter image description here