如何使用我的类数组的变量 [英] how to use the variable of my class array
本文介绍了如何使用我的类数组的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我的代码在这里:
here if my codes:
public:ref class d
{
public:
int i;
};
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
array<d^>^ u = gcnew array<d^>(4);
u[1]->i = 9;
}
然后运行程序,出现NullReferenceException.我不知道为什么以及如何解决它.
我想使用u [0],u [1],u [3]....
then run the program,there comes out a NullReferenceException. I don''t know why and how to fix it.
I want to use u[0],u[1],u[3]....
推荐答案
代码中有一些小错误.它们..您正在创建一个包含4个元素的新数组,但每个元素也需要在使用之前进行初始化...无需将其公开:ref使用public ref代替
Some small misktakes in the code.. I have corrected them.. You are creating a new array with 4 elements but each element also needs to be initialized before using... no need to put public:ref use public ref instead
public ref class d
{
public:
int i;
};
在这里,我为u [1]创建了一个新对象d,然后再使用它.
Here I have crated a new object d for u[1] before using it..
array<d^> ^u = gcnew array<d^>(4);
u[1] = gcnew d;
u[1]->i = 9;
希望这对您有帮助...
Hope this helps...
这篇关于如何使用我的类数组的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文