Trufa
Trufa

Reputation: 40717

How can I share values across classes (or get a variable from another class) without instantiating twice.

My problem is the following, I have a class, let's call it Theatre.

This Theatre has a constructor where I specify the amount Seats that this Theatre has.

The Show class, has an collection of Seats, each with its own properties like bool Empty.

So summing up the code:

class Theatre
{
    public Theatre(int numberOfSeats)
    {
        this.numberOfSeats = numberOfSeats;
    }
}

Lets say we instantiate it somewhere to 100.

Theatre myTheatre = new Theatre(100);

For the Show class:

class Show
{
    List<Seats> listOfSeats = new List<Seats>();
    public Show()
    {
        for (int i = 0; i < 100; i++) //  <---- And here is my problem!!
        {
            //Code to add to list
        }
    }
}

My problem is I don't know to get rid of that 100.

I would like to have something like myTheatre.NumberOfSeats, but I'm not really sure how would that work.

My problem is, I have already instantiated myTheatre in another class, so I would have to make a new theatre inside Show, only to get the number of seats, I would use something like composition and deletagation, but that would clearly violate ISP.

But even that has a problem, since, when I make the new Theatre, I would have to put 100 as a paratmer and, if that were to change, I would have to manually change it.

So my questions are:

As an obvious clarification, the above is not the actual code, just a representation to clarify the situation.

Upvotes: 4

Views: 3849

Answers (3)

PointedEars
PointedEars

Reputation: 14970

C# is not my specialty, so this is purely theoretical; anyhow:

ISTM that myTheatre.numberOfSeats exists already. All you have to do is allow access to it from the outside; either directly, by making its visibility public/package, or (better), by keeping it private/protected and allowing access through a public getter (method).

In class-based OOP you would tell the Show instance about the Theatre instance, so you would pass a reference to the Theatre instance to a method of a Show instance. In this case, the method can be the Show constructor (it is a good idea to keep a default constructor with no arguments, and call it from the special one):

class Show
{
  List<Seats> listOfSeats = new List<Seats>();

  public Show()
  {
  }

  public Show(Theatre theatre)
  {
    this();

    for (int i = 0, len = theatre.numberOfSeats; i < len; ++i)
    {
      /* Code to add to list */
    }
  }
}

Then somewhere:

Theatre myTheatre = new Theatre(100);
...
Show myShow = new Show(myTheatre);

See also: Wikipedia: OOD principles and strategies.

Upvotes: 0

bryanmac
bryanmac

Reputation: 39296

The Show::Show method can take an instance of a Theater as an argument which exposes SeatCount as a property. You could "Show" the same show in two different theaters and the Show class does not need to have any specific knowledge about that theatre.

You also don't want it to be global since a Show can be shown in different theaters. That's the point of OOP.

Another option is to expose a Show method on the Theater class that takes a Show instance as an argument.

Upvotes: 2

parapura rajkumar
parapura rajkumar

Reputation: 24403

This is how you solve it. Pass the Theatre as an argument to Show

 class Theatre
    {
        public Theatre(int numberOfSeats)
        {
            NumberOfSeats = numberOfSeats;
        }
        public int NumberOfSeats { get; private set; }
    }

    class Show
    {
        List<Seats> listOfSeats = new List<Seats>();

        public Show(Theatre theatre)
        {
            for (int i = 0; i < theatre.NumberOfSeats; i++) //  <---- And here is my problem!!
            {
                //Code to add to list
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Theatre myTheatre = new Theatre(100);
            Show myShow = new Show(myTheatre);
        }
    }

Upvotes: 3

Related Questions