views:

1815

answers:

4

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
}
+1  A: 

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.

Joel Coehoorn
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.
John Galt
It's a good time to learn, then. These classes are used a lot.
Joel Coehoorn
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.
John Galt
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.
Joel Coehoorn
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 http://msdn.microsoft.com/en-us/library/512aeb7t.aspx.
Jeff Sternal
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:http://stackoverflow.com/questions/663654/using-namevaluecollection-in-c-webservice-gives-not-xml-serializable-error
John Galt
+1  A: 

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
}
eglasius
You guys are awesome - thank you so much.
John Galt
Exactly what I would've said... (if I had read the question in time!) ;-)
Cerebrus
It should be "nvPairs.Count", though.
Cerebrus
@Cerebrus fixed - if I am in the IDE, it is always one or the other :)
eglasius
+1  A: 

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.

Jeff Sternal
You guys are awesome - thank you so much.
John Galt
A: 

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.

patjbs