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
}
John Adams
  • 4,773
  • 25
  • 91
  • 131

4 Answers4

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 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 Adams Mar 19 '09 at 18:29
  • It's a good time to learn, then. These classes are used a lot. – Joel Coehoorn Mar 19 '09 at 18:34
  • Where can I find good introductory info on this syntax thing with the < > symbols as in List? Every time I see syntax like Dictionary or List I fog over and I need a good intro. – John Adams Mar 19 '09 at 20:07
  • 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 Mar 19 '09 at 20:13
  • 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 Mar 19 '09 at 21:03
  • 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 Adams Mar 19 '09 at 21:18
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
}
eglasius
  • 35,831
  • 5
  • 65
  • 110
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.

Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
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.

patjbs
  • 4,522
  • 3
  • 23
  • 18