C#初始化.对象排成阵列 [英] C# Init. Obj. into array

查看:98
本文介绍了C#初始化.对象排成阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经创建了一个class(Player),现在我想让新对象进入数组.我是这样的:

 Player [] Playerarray =  Player [ 3 ];  

Form1加载:

 Playerarray [ 0 ] =  Player();
Playerarray [ 1 ] =  Player();
Playerarray [ 2 ] =  Player(); 



单选按钮更改事件:

 Playerarray [ 0 ].LabelAktual(); 



LabelAktual()只是将标签标准化为玩家名称.

我现在得到一个证明,我没有为播放器初始化对象.
但是我已经将它放入Form1 Load中,我不明白为什么要得到这个例外.
我做错了什么?

谢谢

解决方案

可能是因为change事件(由于某种原因)在Load事件之前发生. (也许您在代码中选择了单选按钮作为默认按钮?)
将数组init移到构造函数中,或者再次检查CheckChanged事件中是否有值.最好同时做!


hi,

i have created a class(Player) and i want now that new object get into an array. I have do it like that:

Player[] Playerarray = new Player[3];



Form1 Load:

Playerarray[0] = new Player();
Playerarray[1] = new Player();
Playerarray[2] = new Player();



Radiobutton change event:

Playerarray[0].LabelAktual();



LabelAktual() just aktualizes a label into the players name.

I get now an exeption, that i didnt initialize an object for playerarary.
But i have done it into Form1 Load, i dont understand why i get this exeption.
What im doing wrong?

Thank you

解决方案

Probably because the change event happened (for some reason) before the Load event. (Perhaps you are selecting a radio button as a default in your code?)
Either move the array init into the Constructor, or double check in the CheckChanged event that you have a value. Preferably, do both!


这篇关于C#初始化.对象排成阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆