C#を初めて使用しています。ここで私が働いてしまったハードコーディングされたものです:C言語のnamevaluecollectionからオブジェクトの配列を取り込みます。
InputProperty grantNumber = new InputProperty();
grantNumber.Name = "udf:Grant Number";
grantNumber.Val = "571-1238";
Update update = new Update();
update.Items = new InputProperty[] { grantNumber };
今、私は更新オブジェクト内の項目の不特定多数をサポートするために、これを一般化したいと私はこの思い付いたが、それはコンパイルに失敗します。
Update update = BuildMetaData(nvc); //call function to build Update object
、ここで関数自体:
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
}
コンパイラからのエラーメッセージは何ですか? – harpo
割り当てられていないローカル変数 'metaData'の使用 –