Florin Pop
Florin Pop

Reputation: 5135

C# save multiple values

I have a class:

class Entity
{
    private String question;
    private String answer;
    private static int length;

    Crosswords cr = new Crosswords();

    public Entity(int posX, int posY, int direction)
    {
        length = cr.getSpaceLength(posX, posY, direction);
    }

    public int getLength()
    {
        return length;
    }
}

And in another class I am creating an array of Entities like this:

Entity[] et = new Entity[100];

Then I have an algorithm that calculates the length and I want to save the length of each entity, but after that, when I do this:

for(int i=0; i<100; i++)
    Console.WriteLine(et[i].getLength());

I get the same length for all my entities (the length of the last entity), but I set different lengths for them. Why is this happening? What I have to do to save different lengths for each entity?

P.S.

I create the entity like this:

for(int i=0; i<100; i++)
    et[i] = new Entity(somevalueX, somevalueY, direction);

Upvotes: 1

Views: 2283

Answers (3)

Adil
Adil

Reputation: 148110

The length is static member and one variable for all the objects. It should not be static if you want separate for each object.

Change

private static int length;

To

private int length;

Only one copy of a static member exists, regardless of how many instances of the class are created, MSDN.

Calculating the length for all the objects in advance in given situation does not seem worthy. You can calculate and return the length when it is required. I would prefer to make a property for length instead of method.

class Entity
{
   private String question;
   private String answer;
   private int posX;
   private int posY;
   private int direction;
   Crosswords cr = new Crosswords();

  *//Make length a public property*
  public int Length 
  { 
      get { return cr.getSpaceLength(posX, posY, direction);} 
      private set;
  } 

  *//Assign the values to private members for later use.*
  public Entity(int posX, int posY, int direction)
  {
      this.posX = posX;
      this.posY = posY;
      this.direction = direction;
  }
}

Upvotes: 2

Arkadiusz Kałkus
Arkadiusz Kałkus

Reputation: 18363

The cause of this behavior is that you've declared the length field as static.

While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.

https://msdn.microsoft.com/en-us/library/98f28cdx.aspx

Upvotes: 0

Dario Griffo
Dario Griffo

Reputation: 4274

Remove the static from the length or better do this, calculate the lenght when required.

class Entity
{
    private String question;
    private String answer;
    private int _posX;
    private int _posY;
    private int _direction;
    Crosswords cr = new Crosswords();

    public Entity(int posX, int posY, int direction)
    {
        _posX = posX;
        _posY = posY;
        _direction = direction;
    }

    public int getLength()
    {
        return cr.getSpaceLength(_posX, _posY, _direction);
    }
}

Upvotes: 4

Related Questions