0

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] ????

Thank you!

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

Comments

0

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.