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.
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:
..............
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 solution did not work for me as-is (on Visual Studio 2015). The missing step is actually setting or getting from the actual settings. As for the original question, implementing a simple POCO can be achieved like this:
I have used for actually serialization a list:
Instantiate a Schedules (ReportTypeSettings) object. It will automatically read the settings. You can use Reload method to refresh. For instance:
IMPORTANT NOTES:
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.
Just a bit more clear code then Charlie's