Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer peopleโ€™s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer peopleโ€™s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:03:51+00:00 2026-06-14T19:03:51+00:00

Update: Please see EDIT 3 at the bottom. I am very much a beginner

  • 0

Update: Please see EDIT 3 at the bottom.

I am very much a beginner with C# and programming in general (so please be gentle!). I’ve followed plenty of tutorials and read through books and thought I understood classes. Until now.

I have a fairly simple single-class project which uses a 2D string array to set a 10×10 grid of letters E (for empty) and F (for full). I use int x and int y to refer to the coordinates of the array and use switch to detect input and add or subtract from x and y to determine if the array cell is empty or full.

class MainGame
{
public MainGame()
{
    string[,] mapTerr = new string[10, 10]
    {
        { "F", "F", "F", "F", "F", "F", "F", "F", "F", "F" },
        { "F", "E", "E", "E", "E", "E", "E", "E", "E", "F" },
        { "F", "E", "E", "E", "E", "E", "E", "E", "E", "F" },
        { "F", "E", "E", "E", "E", "E", "E", "E", "E", "F" },
        { "F", "E", "E", "E", "E", "E", "E", "E", "E", "F" },
        { "F", "E", "E", "E", "E", "E", "E", "E", "E", "F" },
        { "F", "E", "E", "E", "E", "E", "E", "E", "E", "F" },
        { "F", "E", "E", "E", "E", "E", "E", "E", "E", "F" },
        { "F", "E", "E", "E", "E", "E", "E", "E", "E", "F" },
        { "F", "F", "F", "F", "F", "F", "F", "F", "F", "F" },
    }; // long-winded I know, but helps visualise the array
    int x, y;
    string navDir;
    Console.WriteLine("Enter a command:");
    navDir = Console.ReadLine();
    switch (navDir)
    {
        case "N":
        case "n":
            x -= 1;
            if (mapTerr[x, y] == "F")
            {
                Console.WriteLine("You cannot move North, it is blocked!");
                x += 1;
            }
            else
            {
                Console.WriteLine("You move North.");
            }
            break;
        case "E":
        case "e":
            y += 1;
            if (mapTerr[x, y] == "F")
            {
                Console.WriteLine("You cannot move East, it is blocked!");
                y -= 1;
            }
            else
            {
                Console.WriteLine("You move East.");
            }
            break;
        // etc...

This works fine. However, I’ve tried to split it into separate classes: one for creating the array and another for controlling input and output. This is my attempt:

class Program
{
    public static void Main(string[] args)
    {
        Map map = new Map();
        UserControl usercontrol = new UserControl();
    }
}

class Map
{
    string[,] mapTerr = new string[10, 10] {
    { // array contents here
    };
}

class UserControl
{
    int x, y;
    string navDir;

    public UserControl()
    {
        Console.WriteLine("Enter a command:");
        navDir = Console.ReadLine();
        switch (navDir)
        {
            case "N":
            case "n":
                x -= 1;
                if (mapTerr[x, y] == "F") // ERROR: The name 'mapTerr' does not exit in the current context"
                {
                    Console.WriteLine("You cannot move North, it is blocked!");
                    x += 1;
                }
                else
                {
                    Console.WriteLine("You move North.");
                }
                break;
            // etc...

I can’t for the life of me figure out how to make it work. The errors are primarily when the array is called from within the switch.

I realise this is because the array is associated with the Map class and not the UserControl class, so how do I make it visible / useable?

Despite trawling the array / scope / class sections here and elsewhere online, nothing really explains things entirely in simple terms. I presume it’s a matter of scope and I’m trying to call reference to things in ways that aren’t possible. If anyone can explain what I’m doing wrong and perhaps hint at a way I can go the right way about things, I’d really appreciate it! (Apologies for the long explanation / question)

EDIT 1: added specific error message as a comment beside the line. This occurs on every line the mapTerr is referred to within the switch.

EDIT 2: clarified instantiating and class structure.

EDIT 3: Okay, the string array is set in the Map class publicly, public string[,] elsaNav = new string[10, 10] {{/*contents*/}; and I instantiate the map class in the Program class, but still can’t call the mapTerr array from within UserControl class, despite using map.mapTerr…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:03:53+00:00Added an answer on June 14, 2026 at 7:03 pm

    I’m not quite sure how your final solution looks like after editing with the suggestions given in this post, but I’ve fixed up the code to give you one way of working with it, hopefully it can help you to pinpoint the error in your code ๐Ÿ™‚

    using System.Collections;
    using System;
    
    class Program
    {
        public static void Main(string[] args)
        {
            UserControl usercontrol = new UserControl();
        }
    }
    
    class Map
    {
    // Private map
    private string[,] mapTerr;
    
    // Public map
    public string[,] MapTerr
    {
        get {return mapTerr;}
    }
    
    public Map(int width, int height)
    {
        mapTerr = new string[width, height];
    
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                mapTerr[x,y] = "E";
            }
        }
    }
    }
    
    class UserControl
    {
        int x, y;
        string navDir;
    Map map;
    
        public UserControl()
        {
            map = new Map(10, 10);
            Console.WriteLine("Enter a command:");
            navDir = Console.ReadLine();
            switch (navDir)
            {
                case "N":
                case "n":
                x -= 1;
                if (map.MapTerr[x, y] == "F") // ERROR: The name 'mapTerr' does not exit in the current context"
                {
                    Console.WriteLine("You cannot move North, it is blocked!");
                    x += 1;
                }
                else
                {
                    Console.WriteLine("You move North.");
                }
                break;
            // etc...
            }
    }
    }
    

    My apologies if the indention is a bit off, I’m in a hurry and do not have access to a proper C# compiler right now, but the above code should work ๐Ÿ™‚

    It was a good idea to encapsulate and seperate your map and userinput into two classes, by the way! Great head start for a new programmer ๐Ÿ™‚

    Remember to use properties exensively in your classes to control how data members are accessed in your classes. In most cases you do NOT want to make members purely public. Instead, make them private and make a property for them, so you can control how data can be set or accessed.

    Of course, I’ve just quickly written some example logic for initializing the array with letters just to showcase the solution, so you will probably want to re-write that to suit your own needs ๐Ÿ™‚

    Also, how you want to work with the map object also depends on your own thought about how the client should work with the code, I simply make a Map object a composition of the UserControl class without letting client code work with it. Another way could of course be letting the client code initialize its own map object and pass it into the constructor of the UserInput class.

    Hope it helps you.

    [EDIT]

    Okay, the string array is set in the Map class publicly, public string[,] elsaNav = new string[10, 10] {{/contents/}; and I instantiate the map class in the Program class, but still can’t call the mapTerr array from within UserControl class, despite using map.mapTerr

    Okay, I haven’t seen your solution code for this but perhaps I still get it… let me have a go at it ๐Ÿ˜› Basically you are instantiating the Map class in the Program class, something like:

    Map myMap = new Map();
    

    and then you try to access the “myMap” reference directly in the instance of your UserControl class? In this case, you can’t simply do this. The myMap reference is only in scope within the Program class method that you are calling it from, so when you try to reference it from within your UserControl class it won’t have any knowledge of it whatsoever, it is out of its scope. Instead, you would have to pass a reference into the UserControl class like this:

    Map myMap = new Map();
    
    UserControl newControl = new UserControl(myMap);
    

    and then from within the class your constructor would look like this:

        public UserControl(Map map)
        {
            map.MapTerr... etc
        }
    

    Perhaps I misunderstood what you said, but if that is what you meant, then the above solution should fix it right up ๐Ÿ™‚

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Please see update at bottom) I've looked at dozens of questions and haven't been
How do we update a textbox on ticking checkboxes? Please see the following code,
Please read my update at the end of question after reading the answers: I'm
Edit Please see also How do I properly clean up Excel interop objects? .
Update Added jsfiddle - see bottom of post I currently have a function that
Please look at : http://news.bbc.co.uk/2/hi/europe/8592380.stm The content updates automatically, however, I can't see it
Please help me with this update query. I COMPLETELY understand how redundant this is
I need to update jquery1.3.2 to jquery1.4 (in Asp.net MVC). Please can anyone tell
In vb6 you could see a textbox update immediately when the value was changed,
* Solved, see Update below I have a class with the two properties listed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.