1

I'm really new to C# but I'm trying to make a simple game. In order to make the movement and positioning of instances easy, I'm using an array. The problem is that I can't seem to get it to work.

I get the error message:

'GAArr' doesn't exist in the current context

for details see the Draw method.

//Every part of the world: terrain, enemies, itmes and alike
static void World()
{
    int GAWidth = 78;
    int GAHeight = 25;
    string[,] GAArr = new string[GAWidth, GAHeight]; //said array
    ...
}


//Wall class
class Wall {
    ...

    public Wall(int x, int y, int mh){
        ...
    }

    void Draw() {
        GAArr[x, y] = "#"; //it says the name 'GAArr' doesn't exist in the current context
    }
}

(I'm sorry for copying all the code, but it might make it clearer what I'm trying to do) I've tried some of the solutions like creating a static global class, but that didn't seem to work. The other thing I saw was an Auction class, but (from what I understand) that would take a lot of time and make it harder to access and manipulate the position of instances. Please help.

5
  • 1
    Your "said array" in the code isn't global.... put it outside of the world but inside the class. Commented Sep 4, 2017 at 9:31
  • Global variable scope is limited to class level. Commented Sep 4, 2017 at 9:32
  • You recreate your array each time the Update() is called, so it is not working Commented Sep 4, 2017 at 9:33
  • Define "doesn´t work"? What results do you get and what do you get instead? Commented Sep 4, 2017 at 9:33
  • 1
    You need to declare the array outside of the method. Commented Sep 4, 2017 at 9:34

3 Answers 3

4

GAArr is defined as a local variable in the World() method. It's not accessible from the scope of the nested Wall class.

You might find this useful: C# Language specification - Scopes


Here's a simpler example of what you're trying to do:

public class Outer
{
    public void Draw()
    {
        int[] intArray = new[] { 1, 2, 3 };
    }

    public class Inner
    {
        public void Draw()
        {
            // ERROR: The defined array is not available in this scope.
            intArray[0] = 0;
        }
    }
}

Some other answers have suggested you place the array as a member of the parent class. That doesn't work either:

public class Outer
{
    public int[] IntArray = new[] { 1, 2, 3 };

    public class Inner
    {
        public void Draw()
        {
            // ERROR: As the nested class can be instantiated without its parent, it has no way to reference this member.
            IntArray[0] = 0;
        }
    }
}

There are a number of ways you can address this, including:

  • Pass the array to the Wall.Draw() method as a parameter, or even to the Wall's constructor
  • Define the array as a singleton within a static class, and reference this.
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for quick the response, but I understand that already. I just don't know how to make it global
just declare it outside of a function but inside the class
@Mr.Dude__ I added some more detail, take a look.
1

Your variable is accesible only inside the scope of class. To make variable accesible from other classes you have to either give referrence (eg. in constructor) or make class that will hold this variable

First, make class as below.

static class Globals
{
    public static string[,] GAArr; //Maybe needed to initialize, I dont have access to Vs atm so only I only guess syntax 
}

Then in your world class change

string[,] GAArr = new string[GAWidth, GAHeight]; //said array

into this

Globals.GAArr = new string[GAWidth, GAHeight]; //said array

and in wall class

 void Draw() {
        Globals.GAArr[x, y] = "#";
    }

Comments

0
class Program
{
    static string[,] GAArr; // define in the class, but outside of the functions
    // ...
    static void World()
    {
        // ...
        GAArr = new string[GAWidth, GAHeight]; // create
        // ...
    }
    // ...
}

class Wall
{
    void Draw()
    {
        Program.GAArr[x,y] ? "#"; // Use in another class
    }
}

Be aware, that every use of the array before the initialization will cause an Exeption.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.