Initializing a StringCollection

I am writing an application and I want to keep a list of files selected by the user. Currently, one of my settings is a StringCollection named filesToFetch, which is the user's scope and contains the paths of all the files that the program should extract. I have a button that allows the user to add new files to the list. This is the button click event code

private void button1_Click(object sender, EventArgs e) { DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { Properties.Settings.Default.filesToFetch.Add(openFileDialog1.FileName); Properties.Settings.Default.Save(); } } 

When I try to add a new file to StringCollection, I get an error

NullReference exception was unhandled

The reference to the object is not installed in the instance of the object.

I think this could happen because filesToFetch was not initialized, but I'm not sure. I could be wrong, but I thought that the object gets a name when it is initialized, and since my settings all get names during development, I assumed that they are automatically initialized when the program starts, but now I think I can be wrong about that. Is this a problem, or am I missing something else?

Here is a screenshot of my settings for reference.

Properties settings

+4
source share
3 answers

I should explain a little further. Let's say you are going to use a list of strings. You can declare:

 IList<string> a; 

At this point, a = null and null has no Add method. If you initialize:

 IList<string> a = new List<string>(); 

Now a = empty list of strings. At this point, the Add method will be used to add lines to the list.

+2
source

If you want to enter values ​​in the settings GUI, in the far right corner there is a button "...", which allows you to enter the initial values ​​of the lines, each of which is divided into a line. He then converts this to XML as such:

 <?xml version="1.0" encoding="utf-16"?> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <string>String1</string> <string>String2</string> </ArrayOfString> 

edit: Yes, you need to initialize the StringCollection, and my answer above is a way to do this using the GUI. Thought it would help people (like me) who stumbled upon this post looking for a way to initialize a StringCollection setting such as OP.

+9
source

I had a similar problem using the add method, but inserting with index and value parameters worked fine.

https://msdn.microsoft.com/en-us/library/system.collections.specialized.stringcollection.insert%28v=vs.110%29.aspx

Something like this might work:

 private void button1_Click(object sender, EventArgs e) { DialogResult result = openFileDialog1.ShowDialog(); if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return; int x = 0; foreach (String file in openFileDialog1.FileNames) { Properties.Settings.Default.activeFiles.Insert(x, openFileDialog1.Filename); x++; } Properties.Settings.Default.Save(); } 
0
source

Source: https://habr.com/ru/post/1446481/


All Articles