I too am confised by the documentation.
MSDN States:
Overloads Public Overridable Function GetValues( _
ByVal name As String _
) As String()
Overloads Public Overridable Function Get( _
ByVal name As String _
) As String
It could not be more plain than that. Yet I get the same results as
Vagif.
It appears that either I am not entering the app.config data correctly
or that the get values function does not act as I would expect.
anyone else have any ideas how to save an array of strings in
app.config?
Thanks
Hi,
I've been pounding on a similar issue for a couple of hours and ran into
your posts.
I was finally successfully found a reference at:
http://www.o-xml.org/projects/mlml/cli/System.Collections.Specialized.Na
meValueCollection.html
You need to scroll down a little pass constructor to get to the first
public virtual method Add(string name, string value)
I quote: "If the specified key already exists in the current instance,
the specified value is added to the existing comma-separated list of
values associated with the same key. Attempting to assign the same value
to an existing key adds a new value to that key, thus providing two (or
more) copies of the same value associated with the key."
I've put together a small sample to demonstrate:
public class Class2 {
[STAThread]
static void Main(string[] args) {
NameValueCollection nv = new NameValueCollection();
NameValueCollection nvMulti = new NameValueCollection();
Random r = new Random();
for (int i = 0; i < 3; i++)
nv.Add("itm " + i, r.Next(50).ToString());
// Multi Values NameValueCollection
for (int i = 0; i < 3; i++) {
nvMulti.Add("t" + i, r.Next(1000).ToString()); // add 1st value
nvMulti.Add("t" + i, r.Next(1000).ToString()); // add 2nd value
nvMulti.Add("t" + i, r.Next(1000).ToString()); // add 3rd value
}
for (int i = 0; i < nv.Count; i++)
Console.WriteLine("item[`{0}`] = {1}", nv.GetKey(i),
nv.GetValues(i)[0]);
for (int i = 0; i < nv.Count; i++) {
System.Text.StringBuilder vals = new
System.Text.StringBuilder();
for (int j = 0; j < nvMulti.GetValues(i).Length; j++) {
vals.Append(nvMulti.GetValues(i)[j] + "~");
}
Console.WriteLine("item[`{0}`] = {1}", nvMulti.GetKey(i), vals);
Console.WriteLine("item[`{0}`] = {1}", nvMulti.GetKey(i),
nvMulti.GetValues(i)[0]);
Console.WriteLine("item[`{0}`] = {1}", nvMulti.GetKey(i),
nvMulti.Get(i));
}
}
}
The result is quite interesting:
When adding more than one name/value pair using the same name to a
NameValueCollection, it will create the 'Array' of values (it's actually
is ArrayList).
Printing out the value using Get(i), C# prints comma separated values.
For example, 123,456,789
GetValues(i).Length will print out 3 for this sample which means 3
item in the array.
Good luck, Jeff