views:

285

answers:

4

Hi, In my settings file, I would like to have one setting that is of the type List(of Myclass).... I can't find anything on the net that does this. Is it possible?

A: 

Yes it is possible, just type List<SomeType> into the type field manually, it should work.

Enyra
Where do you type this? In the project's properties in the Settings tab you can't add the field type manually. You have to add one of the predetermined types or browse for a type. (for a VB project)
Meta-Knight
Yeah exactly, you have to browse for a type.
Codezy
Mh, I don't remember exactly how I did this, but somehow I could enter a generic type. If it does not work, I would make a serializable wrapper which inherits from the generic list. At least this should work.
Enyra
This does not work in VS2010, .NET 4.0. Gives error: "Type `System.Collections.Generic.List<SomeType>' is not defined." once you hit "OK".
DGGenuine
A: 

You could serialize your List<T> to a string, and put that in your settings file.

Would that work?

Mike
+3  A: 

I assume you're talking about user settings...

I don't know if you can specify a generic type, although if you create your own non-generic type which inherits from List(of MyClass), then you can specify this type as your user setting without any problem.

Something like:

Public Class MyClassList 
    Inherits List(Of MyClass)

End Class

Then you should be able to browse for MyClassList .

Meta-Knight
This is what my co-worker ended up doing, tks.
Codezy
A: 

I agree that Meta-Knight's inheritance solution is best, however it is possible to do this by manually editing the setting XML file. In Solution Explorer go to: Solution > Project > Properties > Settings.settings. Right-click on Settings.settings and choose "Open With". Select "XML (Text) Editor". Then insert the following inside <Settings>:

<Setting Name="TestGenericList" Type="System.Collections.Generic.List&lt;int&gt;" Scope="User">
  <Value Profile="(Default)" />
</Setting>

save, and it works.

Unfortunately it doesn't appear to work with generic types requiring two or more types, since the comma is not allowed in that attribute. Even entity-encoding it (&#x002C;) didn't help.

The following causes an error "Illegal characters in path" when you go to edit the settings file in the settings designer:

<Setting Name="TestGenericDictionary" Type="System.Collections.Generic.Dictionary&lt;string&#x002C;string&gt;" Scope="User">
  <Value Profile="(Default)" />
</Setting>
DGGenuine