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 }
Since the size of your Items collection can vary, you should use a collection type like
List<T>orDictionary<K,V>instead of an array.