Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

c# - How can a class be accessible for one form, and not for the other?

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.

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).

This will give an IndexOutOfRange exception:

private Species species = new Species();
int i = this.species.size[1];

This is working though:

int i = UIForm.species.size[1];

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?

I was used to be able to access the species class from form1 before i had made the UIForm.

I'm sorry if I am too vague or if it's very obvious, but I am still a noob at this.

(B.T.W. There is no threading going on)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Or better yet

private Species GetUISpecies()
{
    return UIForm.species;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...