views:

572

answers:

3

I have a small class that holds two strings as follows:

    public class ReportType
    {
        private string displayName;
        public string DisplayName
        {
            get { return displayName; }
        }

        private string reportName;
        public string ReportName
        {
            get { return reportName; }
        }

        public ReportType(string displayName, string reportName)
        {
            this.displayName = displayName;
            this.reportName = reportName;
        }
    }

I want to save an instance of this class to my settings file so that I can do the following:

ReportType reportType = Settings.Default.SelectedReportType;

Googling seems to suggest that it is possible but there doesn't appear to be a clear guide anywhere for me to follow. I understand that some serialization is required but don't really know where to begin. Also, when I go into the Settings screen in Visual Studio and click "browse" under the Type column there is no option to select the current namespace which contains the ReportType class.

+1  A: 

How about creating a static method which returns an instance of ReportType containing data from the config file. It's simpler and I don't think serializing is necessary.

public class ReportType
{
  public static ReportType GetDefaultSelectedReportType()
  {
    string displayName = ConfigurationManager.AppSettings["DefaultDisplayName"];
    string reportName = ConfigurationManager.AppSettings["DefaultReportName"];
    return new ReportType(displayName, reportName);
  }
  .
  .
  .
}
Charlie
I could do that as a last resort but I actually have a number of different reports to save settings for and I will probably also be increasing the ReportType class to store more strings than the two shown here.
Calanus
Have a settings helper class with extension methods for each report then? A serialized object in a config will look a bit wierd I think.
Charlie
+1  A: 

Just a bit more clear code then Charlie's

public class ReportType
{
  public static ReportType CreateDefaults()
  {
    return new ReportType
    {
       DisplayName =  ConfigurationManager.AppSettings["DefaultDisplayName"],
       ReportName = ConfigurationManager.AppSettings["DefaultReportName"]
    };
  }
}
abatishchev
alright smart arse ;)
Charlie
Actually I'm afraid I'm going to undo my upvote. Since this object is supposed to hold configuration data, I think it should be immutable so that it's state cannot be changed after construction. So my answer is better ;)
Charlie
+2  A: 

OK I think that I have eventually worked it out. The first thing to do is to add the following attributes to each property of the ReportType class that needs to be serialised and inherit the class from ApplicationSettingsBase:

    public class ReportType : ApplicationSettingsBase
    {
        private string displayName;
        [UserScopedSetting()]
        [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
        public string DisplayName
        {
            get { return displayName; }
        }

..............

and then, once you have rebuilt your assembly (important!) you can go into the settings screen and click browse and then type your namespace and class name in the text box at the bottom (e.g. Label_Creator.ReportType). The namespace and class name do not appear in the tree and so this part is not exactly obvious what you need to do which is why it is a bit confusing....

Calanus