开发者

How to declare "global" variable in OOP project?

suppose you've this code:

namespace StighyGames.CarsAttack {


    public class CarsAttack

   {    


     public static Channel[] ch = new Channel[30];      
   ...
    }
void main {
   CarsAttack game = new CarsAttack();
}

}

In another cs file on the same project i declare another Class ...

public class AnotherClass {
   void AFunction() {
      ch[1] = .. something;
    }
}

Error: the name ch doesn't exists in current context !

How can i access to game.ch[index] ????

T开发者_开发知识库hank you!


How can i access to game.ch[index] ????

CarsAttack.ch[index];

Its impossible to access variables without the qualifications from a different class or namespace. They only exist in the method/class they are declared. You have to fully qualify static access with the name of the class (and namespace as well if you're in a different one). :D


As ch is a public member, you can access it by CarsAttack.ch. But, however, maybe you should refactor your design (not using statics/singletons) and naming (ch: wtf?)... ;)


By supplying the game object to the other class and using it as an instance variable.


Try

CarsAttack.ch[1] = something;

though this is bad design. You might have to make CarsAttack static too.

What are you actually trying to achieve?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜