2010-04-06 64 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; 
} 

希望帮助