Home > OS >  How do I store both objects and ints in an array?
How do I store both objects and ints in an array?

Time:11-11

I'm currently working on a project where I have to transform a warehouse into an array representation, however, I am running into a problem. I want to define the path where the workers can walk as an arraycell with a value = 1. Places, where the workers can't go, will have a value of 0, and the shelves with items will be Object. This is of course not a viable option, as we can't store objects in an int array, however, I currently can't think of another way to represent pathways other than using int arrays. So how would one go about doing this? Here's a quick visual representation of what I'm looking for: Array example

namespace MyApplication
{
    
  class MagazijnObject
  {
    public string Locatie;//waar ligt het product?
    public int Ranking; //hoe populair is dit product?
    public string Item; //welk product ligt hier?

    
    public MagazijnObject(string loc, int rank, string it)
    {
     Locatie = loc;
     Ranking = rank;
     Item = it;
    }
     

    static void Main(string[] args)
    {
      MagazijnObject voorbeeld = new MagazijnObject("D02020A", 1, "Meel");
      Console.WriteLine(voorbeeld.Locatie);
      Console.WriteLine(voorbeeld.Ranking);
      Console.WriteLine(voorbeeld.Item);
      int[,] GangVoorbeeld = new int[5,10]; //this is a single hallway, with shelves on either side
      string gang = "D02";
//traverse array
      for (int i = 0; i < GangVoorbeeld.GetLength(0); i  )
      {for (int j = 0; j < GangVoorbeeld.GetLength(1); j  )

      { if ( i == 1 || i == 3) //shelves with items
      {GangVoorbeeld[i,j] = new MagazijnObject(gang   i, 1, "test"); //try to create object, however can't do so because I have an int array. However, I don't know how to define the walking path in an object representation.
      }
      
      }
      
      
    }
  }
}}

Any tips would be appreciated! Kind regards, Douwe Brink

CodePudding user response:

If you're set on an array, you can make it an array of some common parent of what you want to use.. The simplest would of course be an object[][], but the thing that is tripping you up is this fixation on making a walkway an int and a shelf an object.. It seems more sensible to model everything in a warehouse as some kind of derivative of a base class, rather than have walkways be ints of 0/1..

class WarehouseEntity {} 

class Wall:WarehouseEntity{}
class Walkway:WarehouseEntity{}
class Shelf:WarehouseEntity{}


var warehouse = new WarehouseEntity[5,10] ...

Now you can put a wall, walkway or shelf anywhere in your array.. A walkway could have a list of Person that are currently standing in that 10 square meters (or whatever) of Walkway. A Shelf could have a list of products it contains etc. A wall doesn't do much, but it could be handy to record as a wall because maybe you just want to have all kinds of WarehouseEntity have a Bitmap graphic of what they are, and rendering them is just looping over the warehouse array asking each element for its bitmap, rather than saying "if it's a wall, draw a wall, if it's a walkway, draw a walkway.."

class WarehouseEntity {
  Point Coordinates {get; set;}
  abstract Bitmap GetBitmap();
} 

class Wall:WarehouseEntity{

  override Bitmap GetBitmap(){ return _wall_bmp; }
}

class Walkway:WarehouseEntity{

  public List<Person> PeopleStandingHere;
  override Bitmap GetBitmap(){ return _walkway_bmp; }
}

class Shelf:WarehouseEntity{

  public List<Product> ProductsOnShelf;
  override Bitmap GetBitmap(){ return _walkway_bmp; }
}

...
foreach(var we in warehouse)
{
  graphics.DrawImage(we.GetBitMap(), we.Coordinates.X, we.Coordinates.Y);
}

..polymorphism, baby! :)

  • Related