0

I am trying to practice OOP. I have a class called Cinema and interface called location. Location Interface has List type and when I am trying to add the location name, It is giving error. Any direction or suggestion will be great -

The error is: "The name 'cinema_location' does not exist in the current context."

namespace MOSS.Interfaces
{
    interface loc
    {
        List<string> location { get; set;}
    }
}


namespace MOSS
{
    class Cinema : Interfaces.loc
    {

        List<string> cinema_location = new List<string>();

        cinema_location.Add("location1");
        cinema_location.Add("location2");
        public List<string> location
        {
            get
            {
            return cinema_location;
            }

            set
            {
                cinema_location = value;
            }
        }

        public void DisplayCinema()
        {
            string loc;
            string Session;
            for(int i = 0; i < cinema_location.Count; i++)
            {
                loc = cinema_location[i];
                Console.WriteLine("Cinema Location: {0}", loc);

            }
        }
    }
}
2
  • What is the error? Commented May 30, 2017 at 5:48
  • 1
    Your method calls aren't within a method. cinema_location.Add("location1"); can't just be in the body of a class. Commented May 30, 2017 at 5:48

3 Answers 3

2

A code like this one

cinema_location.Add("location1");
cinema_location.Add("location2");

can't be put wherever you like. In particular, it has to be put in a method, could be a class constructor.

Try

public class Cinema
{
  List<string> cinema_location = new List<string>();

  public Cinema()
  {
     cinema_location.Add("location1");
     cinema_location.Add("location2");
  }

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

Comments

1

Your c cinema_location.Add(...)s have to be inside a method (you cannot execute code directly in class scope).

It seems like you want these names added at object creation, right? In that case you need a constructor and add thr method calls there:

class Cinema
{
    List<string> cinema_locations = new List<string>();
    public Cinema()
    {
         cinema_locations.Add("location1");
    }
}

2 Comments

What if I put it in a new method called addLocation instead of constructor and also define that method in the loc interface as well.Is that will be a good way to structure this!!!!
of course you could implement a addLocation method. either you force it in the interface directly or you give the 'user' of the interface the choice of how to fill the list.
1

You need a constructor (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors):

Public Cinema()
{
    cinema_location.Add("location1");
    cinema_location.Add("location2");
}

1 Comment

actually it is called a constructor

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.