一个类如何可以访问一个窗体,而不是另一个窗体? [英] How can a class be accessible for one form, and not for the other?
问题描述
我正在做一个非常基本的2d(自上而下)的游戏。因此我使用2种形式。一个用于UI(UIForm),另一个用于游戏的可视部分(Form1,也是要加载的第一个表单)。此外,我有一个类,它定义了不同的动物种类,并提供属性像大小。
I am making a very basic 2d (top-down) "game". Therefore I use 2 forms. One for the UI (UIForm) and the other one for the visual part of the game (Form1, also the first form to be loaded). Also I have a class wich defines different animal species and provides properties like the size.
但是,似乎我无法访问Spec1类从我Form1,
However, it seems like I cannot access the Species class from my Form1 since I have created UIForm (which has access to the species class).
这将产生一个IndexOutOfRange异常:
This will give an IndexOutOfRange exception:
private Species species = new Species();
int i = this.species.size[1];
这是有效的:
int i = UIForm.species.size[1];
这里我在UIForm中为了测试而使用了公共的类, 。这工作,所以我可以解决它,但如何解决这个问题?为什么会发生这种情况?
Here I have made species public in UIForm for the sake of testing and used that to access the class Species. This works, so I could work around it, but how do I fix this? And why does this happen?
我曾经能够在我制作UIForm之前从form1访问物种类。
I was used to be able to access the species class from form1 before i had made the UIForm.
对不起,如果我太模糊或者很明显,但我仍然是这个的noob。
I'm sorry if I am too vague or if it's very obvious, but I am still a noob at this.
(BTW没有线程正在进行)
(B.T.W. There is no threading going on)
推荐答案
它看起来好像物种是一个属性/字段在 UIForm
,并且当您在下一个表单中使用它时,您将一个新
实例分配给变量。
it looks as though species is a property/field on UIForm
and when you are using it in your next form, you are assigning a new
instance of species to a variable.`
private Sepcies species = new Species() // wrong
private Species species = UIForm.species // possibly right
或更好
private Species GetUISpecies()
{
return UIForm.species;
}
这篇关于一个类如何可以访问一个窗体,而不是另一个窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!