lundi 28 février 2022

How to get property value of object which is in object array from another class

I want to get property value of an object which is in object array from another class. I tried to use reflection, but I had to do something wrong...

My code:

  //Map.cs

    //in BoardTiles i keep objects of classes which have property "Name" and I want to get it.

    public class Map
        {
            public int Rows { get; private set; }
            private int Cols { get; set; }
            public Object[,] BoardTiles { get; private set; }
            private Floor Floor { get; set; }
            private PlayerTile PlayerTile { get; set; }
            private Sword OldSword { get; set; }
    
    
    
            public Map()
            {
                this.Rows = 10;
                this.Cols = 40;
                this.BoardTiles = new Object[Rows, Cols];
                this.Floor = new Floor();
                this.PlayerTile = new PlayerTile();
                this.OldSword = new Sword("oldSword", 5);
    
            }
//Sword.cs 
//this is what the sword class looks like, object of this class is stored in BoardTiles 

{
    public class Sword : Tile
    {
        public override string Type { get; set; }
        public string Name { get; set; }
        public int Dmg { get; set; }

        

        public Sword(string name, int dmg)
        {
            this.Type = "sword";
            this.Name = name;
            this.Dmg = dmg; 
        }
    }
}

//Program.cs
 case ConsoleKey.Spacebar:
            Console.Clear();

            map.getItem(playerPositionX, playerPositionY);
/*            map.movefill(playerPositionX, playerPositionY);
*/            map.printBoard();
            Console.Write(map.BoardTiles[playerPositionX, playerPositionY]);
            **//Place where i need value of property of object from array of objects** 
            break;

    }
}

I'm doing a dungeon crawler and when the player steps over a tile with an item he can pick the item, so the situation is - player is on the item tile which is an index in an array of objects and I want to know the name of this object. 





Aucun commentaire:

Enregistrer un commentaire