tags:

views:

493

answers:

2

I am trying to store a Type of System.Collection.Queue, the queue will only contain strings. The issue I am having is that when I attempt to use it, it is always Null.

Settings.Default.CorrectionsDescription.Enqueue(textString);

I get the following error:

Object reference not set to an instance of an object.

I have gotten StringCollection to work fine but I need the Queue for FIFO.

How do you initialized the Queue in the Settings or thru Code? When i've tried it has given me the error that it is read only.

A: 

I have always just used:

if (Settings.Default.CorrectionsDescription == null)
{
    Settings.Default.CorrectionsDescription = new Queue();
}

at the program startup, but I would like to know of a better way as well.

tster
I can't get this to work either... I would move on if it would. I keep getting the error. Property or indexer 'ConfigFilesTest.Properties.Settings.CorrectionsDescription' cannot be assigned to -- it is read only
Billy
Perhaps the scope of your setting is set to "Application"... try to change it to "User", it will be writable
Thomas Levesque
+1  A: 

Settings usually only store data in very simple objects or collections (xml-serializable, even Dictionary<TKey, TValue> already causes problem!).

Queue has special semantics and is not very suitable for data storage.

I suggest to use List<string> or StringCollection to store settings and create Queue on start of your application based on that collection.

Saving is the same but in reverse order - create List from your Queue, put it into Settings class and save.

Update:

Standard settings mechanism uses XML serialization for object persistence.

Object implementing IEnumerable<T> must also have method Add(T value) to be xml-serializable. Queue<T> doesn't have such method so can't be successfully deserialized.

MSDN:

XmlSerializer can process classes that implement IEnumerable or ICollection differently if they meet certain requirements. A class that implements IEnumerable must implement a public Add method that takes a single parameter

Konstantin Spirin
This is actually how I've implemented it so far but couldn't figure out why the Queue wouldn't work. It would simplify things if it would, but I understand a little more now after reading on xml-serializable. Thanks Konstantin
Billy
Added info why Queue is not serializable. I think that's the main problem when you are trying to use it in Settings.
Konstantin Spirin
All I'm storing in the queue is strings. No other types of objects. But I haven't been able to add anything to the Queue because it is Null. I can't get it to initialize using the Settings.setting file at design time, nor can i do it at run time.
Billy