[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

static use doubt

I do not understand why in the code below I can have the statement
 
*** private Person passenger;     without using the static keyword

and in the statement

*** private  static Elevator elevatorA;  if I delete the static keyword I get a compiler error

I am a bit confused with the use of static .....






using System;

class Elevator
{
      private int currentFloor = 1;
      private int requestedFloor = 0;
      private int totalFloorsTravelled = 0;
      private Person passenger;
      
      public void LoadPassenger()
      {
            passenger = new Person();                        
      }
      
      public void InitiateNewFloorRequest()
      {
            requestedFloor = passenger.NewFloorRequest();
            Console.WriteLine("Departing floor: " + currentFloor + " Travelling to floor: " +  requestedFloor);
            totalFloorsTravelled = totalFloorsTravelled + Math.Abs(currentFloor - requestedFloor);
            currentFloor = requestedFloor;
      }
      
      public void ReportStatistic()
      {      
            Console.WriteLine("Total floors travelled: " + totalFloorsTravelled);
      }
}

class Person
{
      private System.Random randomNumberGenerator;
      
      public Person()
      {
            randomNumberGenerator = new System.Random();
      }
      
      public int NewFloorRequest()
      {
            return randomNumberGenerator.Next(1,30);
      }
}

class Building
{
      private  static Elevator elevatorA;
      
      public static void Main()
      {
            elevatorA = new Elevator();
            
            for(int i = 0; i == 5; i ++)
            {
                  elevatorA.InitiateNewFloorRequest();
            }
      }
}
0
brillox
Asked:
brillox
  • 2
1 Solution
 
WinterMuteUKCommented:
Hi brillox,

The compiler error you get occurs because you would be attempting to use an instance variable from a static method (Main).
You can only access static methods/members from within a Static method, however you can access static methods/members from within an instance method. An instance method is one that can only be accessed when a class is instanced (i.e. MyClass myClass = new MyClass()).

Does that make sense?

Cheers

Wint.

If your building class was like this (below) it would work fine with a non-static member variable.

  class Building
  {
    private Elevator elevatorA;
    public Elevator ElevatorA { get { return elevatorA; } set { elevatorA = value; } }
    public Building(){}

    public static void Main(){
      Building building = new Building();
      building.ElevatorA = new Elevator();

      for(int i = 0; i == 5; i++)
          building.ElevatorA.InitiateNewFloorRequest();
    }
0
 
WinterMuteUKCommented:
Infact to change building easier:

  class Building
  {
    private Elevator elevatorA;

    public static void Main(){
      Building building = new Building();
      building.elevatorA = new Elevator();

      for(int i = 0; i == 5; i++)
          building.elevatorA.InitiateNewFloorRequest();
    }

You don't need the property to access the member variable, nor the default constructor...! D'oh!

Wint
0
 
Uday EthirajuluCTOCommented:
Helloo,
             we can use only the static Methods/Members with in the Main method. try the following example.

     class tmp
     {
          // it will raise an error because the variable should be static
          // static int x = 10;
                int x=10;

           public static void Main()
           {
                 Console.WriteLine(x);
            }
     }
                   
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now