Mr.Dude __
Mr.Dude __

Reputation: 37

C# global array won't work

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.

Upvotes: 1

Views: 2284

Answers (3)

Daniel May
Daniel May

Reputation: 8226

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.

Upvotes: 4

Emaro
Emaro

Reputation: 1497

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.

Upvotes: 0

pajamac
pajamac

Reputation: 113

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] = "#";
    }

Upvotes: 1

Related Questions