AMP said:
Here is the class I am trying to use. I am able to create the xml file
with an xml writer and deserialize it.When I deserialze it "this" gets
populated like a dictionary, in fact thats what I think it is, but I
cant seam to use the aformentioned Dictionary methods. I know I'm
looking at it wrong, but I'm not sure why.
Unfortunately, you still have not posted a complete code example.
However, there's enough in the code you did post to know what the
specific problem is: you have declared the "SerializableDictionary"
class as a generic type, but you are attempting to use a specific,
concrete version of the generic "Dictionary" class in your "for test
purposes" code.
So, when you compile "SerializableDictionary<TKey, TValue>", the type
parameters are just that: type parameters. They are not yet actual
types. But of course the "Picks" variable uses specific type arguments,
int and string[]. There being no valid conversion from the
unconstrained generic type arguments TKey and TValue to the specific
types int and string[], you get a compiler error.
Frankly, it's not at all clear why you are even doing what you're trying
to do with that code. What "test purposes" are served with that local
variable? Given that the code you are showing should, without the
"Picks" variable at all, work just fine, what's the point?
If you really want to include some kind of test code, you will need to
make that part of the code generic as well. For example, changing the
declared type of "Picks" from "Dictionary<int, string[]>" to
"Dictionary<TKey, TValue>" would allow the call to "Picks.Add()" to
work. Likewise, if you had declared "foos" as "TValue[]", the following
statement calling "this.Values.CopyTo()" should work as well.
On that second statement, note that even if your SerializableDictionary
were not generic, and simply inherited Dictionary<int, string[]>, the
declaration for "foos" would not have been valid. As the "Values"
property is a collection of the value type, and the value type is itself
an array string[], the type of the destination array for the call to
"CopyTo()" has to be "string[][]", not "string[]".
Additionally, you initialize that array to be the length of the
dictionary on entering the "ReadXml()" method, but of course at that
point the length is 0. Why you're trying to copy all the values from
the dictionary to an array each time through the loop, I don't know.
But for that to work, you need to make sure that the destination array
"foos" is large enough to contain all the values _currently_ in the
dictionary when you make the call. The simplest approach being to
simply reallocate the array each time through the loop; of course,
that's inefficient, so you could instead use some kind of
doubling-in-size approach. In any case, the destination array must be
large enough to contain all the values from the Values property
collection, or else an exception will be thrown when you call CopyTo().
Pete