2010-04-06 97 views
0
public value struct ListOfWindows 
{ 
HWND hWindow; 
int winID; 
String^ capName; 
}; 

現在這就是我的結構我已經創造了他們的數組:陣列結構CLI的

array<ListOfWindows ^>^MyArray = gcnew array<ListOfWindows ^>(5); 

現在來測試其是否正常工作我做了一個簡單的函數:

void AddStruct() 
{ 
    HWND temp = ::FindWindow(NULL, "Test"); 
    if(temp == NULL) return; 
    MyArray[0]->hWindow = temp; // debug time error.. 

    return; 
} 

錯誤: An unhandled exception of type 'System.NullReferenceException' occurred in Window.exe

Additional information: Object reference not set to an instance of an object. 

不知道該幹什麼d o ..有點新的CLI,所以如果你能幫助請做.. 謝謝。

+0

你不分配數組元素,你剛纔分配陣列 – 2010-04-06 10:43:41

回答

0

那麼,你有一個對象的引用數組,但我沒有看到任何代碼實際上將對象放入其中任何一個。訪問MyArray[0]之前,你可能想將物體放入數組的位置0

我會改變ListOfWindows是一個引用類 - 在你的背景下,並沒有太大的意義,把它作爲一個值類 - 和那麼你可以像這樣添加一個對象到這個數組:

MyArray[0] = gcnew ListOfWindows; 

(未經測試,但這或多或少應該如何工作)。一旦你真的添加了這個對象,你就可以與之交互了。

+0

你是什麼puting的objent到這樣的陣列是什麼意思? MyArray [0] = gcnew ListOfWindows ^; ?? – Nitroglycerin 2010-04-06 11:24:53

+0

@Nitroglycerin - 見上面的擴展答案。 – 2010-04-06 12:56:51

0

首先,您正在創建一個不是數組值的引用數組,因爲@ timo-geusch說您需要在創建引用數組後創建這些對象。

但是,你也可以創建一個像這樣的值的數組。

array<ListOfWindows>^ MyArray = gcnew array<ListOfWindows>(5); 

然後,你可以使用這些值來訪問這些值。運算符而不是 - >運算符,就像這樣。

void AddStruct() 
{ 
    HWND temp = ::FindWindow(NULL, "Test"); 
    if(temp == NULL) return; 
    MyArray[0].hWindow = temp; // << here you access the value type, not the reference 
    return; 
} 

希望幫助