Populate array of objects from namevaluecollection in c#
Asked Answered
I

4

1

I am new to C#. Here is a hard-coded thing I got working:

InputProperty grantNumber = new InputProperty();
grantNumber.Name = "udf:Grant Number";
grantNumber.Val = "571-1238";

Update update = new Update();
update.Items = new InputProperty[] { grantNumber };

Now I want to generalize this to support an indefinite number of items in the Update object and I came up with this but it fails to compile:

Update update = BuildMetaData(nvc);  //call function to build Update object

and the function itself here:

private Update BuildMetaData(NameValueCollection nvPairs)
{
    Update update = new Update();
    InputProperty[] metaData;       // declare array of InputProperty objects
    int i = 0;
    foreach (string key in nvPairs.Keys)
    {
        metaData[i] = new InputProperty();      // compiler complains on this line
        metaData[i].Name = "udf:" + key;
        foreach (string value in nvPairs.GetValues(key))
            metaData[i].Val = value;
    }
    update.Items = metaData;
    return update;      // return the Update object
}
Iolanthe answered 19/3, 2009 at 18:22 Comment(2)
What's the error message from the compiler?Bani
Use of unassigned local variable 'metaData'Iolanthe
O
3

Since the size of your Items collection can vary, you should use a collection type like List<T> or Dictionary<K,V> instead of an array.

Octarchy answered 19/3, 2009 at 18:26 Comment(6)
I tried to show at the beginning of my question how the update.Items needs to be assigned an array of InputProperty objects. I don't understand List and Dictionary yet.Iolanthe
It's a good time to learn, then. These classes are used a lot.Octarchy
Where can I find good introductory info on this syntax thing with the < > symbols as in List<T>? Every time I see syntax like Dictionary<K,V> or List<T> I fog over and I need a good intro.Iolanthe
The real short version is that the T, K, or V are just place holders for a type name like string or int. When you put the type name in there, you're telling the class that it only works with items of that type.Octarchy
I'd just like to add my humble concurrence - it's a terrific idea to learn about List<> and Dictionary<>. They are invaluable (and omnipresent) tools. Of the many places you can learn about generics - classes that use the <> construct - I like msdn.microsoft.com/en-us/library/512aeb7t.aspx.Rayborn
Thanks for all these suggestions. Generics is moving to the top of my "study" stack. BUT - now I run into problem serializing my NameValueCollection through my web service: #664154Iolanthe
B
2

For the current compiler error, you need to initialize the metaData array, like:

InputProperty[] metaData = new InputProperty[](nvPairs.Count);

Using linq you could:

private Update BuildMetaData(NameValueCollection nvPairs)
{
    Update update = new Update();
    update.Items = nvPairs.Keys
        .Select(k=> new InputProperty
                    {
                       Name = "udf:" + k,
                       Val = nvPairs[k] // or Values = nvPairs.GetValues(k)
                    }
         )
        .ToArray();
    return update;      // return the Update object
}
Bagley answered 19/3, 2009 at 18:29 Comment(3)
Exactly what I would've said... (if I had read the question in time!) ;-)Derwent
It should be "nvPairs.Count", though.Derwent
@Derwent fixed - if I am in the IDE, it is always one or the other :)Bagley
R
1

If I'm not mistaken, your InputProperty array is never initialized. If you change line 2 to this:

InputProperty[] metaData = new InputProperty[nvPairs.Count];

That should fix it.

Rayborn answered 19/3, 2009 at 18:29 Comment(0)
S
0

When you declared your array InputProperty[] metaData, you didn't initialize it. So when you tried to access a member, it just doesn't exist, which is why you got the error you did.

As Joel recommended, I'd advise you to look at the collection types provided in System.Collections.Generic for something suitable.

Sonyasoo answered 19/3, 2009 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.