我想創建一個包含我正在處理的所有圖釘對象的數組。當試圖填充數組時,我得到一個NullReferenceException拋出的未處理錯誤。我已經閱讀了儘可能多的文檔,並且無法弄清楚發生了什麼。NullReferenceException與C中的數組#
我已經試過至少有以下:
Pushpin[] arrayPushpins;
int i = 0;
foreach (Result result in arrayResults)
{
Pushpin pin;
pin = new Pushpin();
pin.Location = d;
myMap.Children.Add(pin);
arrayPushpins[i] = new Pushpin();
arrayPushpins.SetValue(pin, i);;
i++;
}
AND ...
Pushpin[] arrayPushpins;
int i = 0;
foreach (Result result in arrayResults)
{
Pushpin pin;
pin = new Pushpin();
pin.Location = d;
myMap.Children.Add(pin);
arrayPushpins[i] = new Pushpin();
arrayPushpins[i] = pin;
i++;
}
而且似乎沒有任何工作。我每次都遇到了NullReference錯誤。 任何想法? 非常感謝! 請問。
爲什麼不使用IEnumerable集合呢? –
剛剛添加了這個,謝謝:-) –
初始化固定的數組。奇怪的是,我以爲我也嘗試過,但可能不會與使用SetValue方法結合使用。謝謝,阿米泰。我也會嘗試使用一個Collection,因爲我認爲你是對的 - 在這種情況下會更好,因爲引腳數量可能會有所不同。 –