结构数组 CLI
public value struct ListOfWindows
{
HWND hWindow;
int winID;
String^ capName;
};
这就是我的结构,现在我已经创建了它们的数组:
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;
}
错误:Window.exe 中发生了类型为“System.NullReferenceException”的未处理异常
Additional information: Object reference not set to an instance of an object.
不知道该怎么办..对 CLI 有点陌生,所以如果你能帮忙,请做.. 谢谢。
public value struct ListOfWindows
{
HWND hWindow;
int winID;
String^ capName;
};
thats my structure now i have created an array of them:
array<ListOfWindows ^> ^ MyArray = gcnew array<ListOfWindows ^>(5);
now to test if that works i made a simple function:
void AddStruct( )
{
HWND temp = ::FindWindow( NULL, "Test" );
if( temp == NULL ) return;
MyArray[0]->hWindow = temp; // debug time error..
return;
}
ERROR: An unhandled exception of type 'System.NullReferenceException' occurred in Window.exe
Additional information: Object reference not set to an instance of an object.
dont know what to do.. kinda new to CLI so if you can help please do..
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您有一个对象引用数组,但我没有看到任何实际将对象放入其中的任何代码。在访问
MyArray[0]
之前,您可能想将一个对象放入数组的位置 0 处。我会将
ListOfWindows
更改为引用类 - 在您的上下文中它不会将它用作值类没有多大意义 - 然后您可以将一个对象添加到数组中,如下所示:(未经测试,但这或多或少是它应该如何工作的)。一旦您实际添加了该对象,您就可以与它进行交互。
Well, you have an array of references to objects but I don't see any code that actually puts an object into any of them. Before accessing
MyArray[0]
you might want to put an object into the array at position 0.I would change
ListOfWindows
to be a ref class - in your context it doesn't make much sense to use it as a value class - and then you can add an object to the array like this:(Untested but that's more or less how it should work). Once you've actually added that object you can then interact with it.
首先,您要创建一个引用数组,而不是一个值数组,因此正如 @timo-geusch 所说,您需要在创建引用数组后创建这些对象。
但是,您也可以创建这样的值数组。
然后您可以使用 .运算符而不是 ->运算符,像这样。
希望有帮助
First you're creating an array of references not an array of values, so as @timo-geusch says you need to create those objects after creating your array of references.
However you could also create an array of values like this.
Then you'd access those values with the . operator instead of -> operator, like this.
Hope that helps