CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2013
    Location
    Georgia
    Posts
    4

    Angry A little array issue

    So I'm pretty sure all this is syntactically correct. What I've done is create a struct object to used as an array on another object. (An array of objects within an object if you will)

    What I have here is this
    Code:
     public struct Tile
        {
            private const int MAXLAYERS = 4;
            private const int MAXDATA = 5;
            private int tileAttribute;
            private static int[] tileLayer = new int[MAXLAYERS];
            private static object[] tileData = new object[MAXDATA];
    
            //this method is used to update the tile's graphic, given a specific layer
            public void updateTile(int layer, int tile)
            {
                tileLayer[layer] = tile;
            }
    
            // overloaded updateTile, allowing you to update just the attribute
            public void updateTile(int attribute)
            {
                tileAttribute = attribute;
            }
    
            //overloaded updateTile, allows you to update the tileData of a tile, given a dataIndex
            public void updateTile(int dataIndex, object newData)
            {
                tileData[dataIndex] = newData;
            }
    
            public int getTileLayer(int layer)
            {
                return tileLayer[layer];
            }
    
            public int getTileAttribute()
            {
                return tileAttribute;
            }
    
            public object getTileData(int dataIndex)
            {
                return tileData[dataIndex];
            }
    
            public int getMaxLayers()
            {
                return MAXLAYERS;
            }
    
            public int getMaxData()
            {
                return MAXDATA;
            }
        }
    
        public class Map
        {
            private const int MAXMAPX = 20;
            private const int MAXMAPY = 15;
    
            public Tile[,] mapTile = new Tile[MAXMAPX, MAXMAPY];
    
            // Messy blank map ctor
            public Map()
            {
                // nested for loops to make all tiles blank
                for (int x = 0; x <= MAXMAPX - 1; x++)
                {
                    for (int y = 0; y <= MAXMAPY -1; y++)
                    {
                        for (int z = 0; z <= mapTile[x,y].getMaxLayers() - 1; z++)
                        {
                            mapTile[x, y].updateTile(z, 0);
                        }
                    }
                }
    
                // nested for loops to make all map data blank
                for (int x = 0; x <= MAXMAPX - 1; x++)
                {
                    for (int y = 0; y <= MAXMAPY - 1; y++)
                    {
                        for (int z = 0; z <= mapTile[x, y].getMaxData() - 1; z++)
                        {
                            mapTile[x, y].updateTile(z, "");
                        }
                    }
                }
    
                // for loop to make all attributes blank
                for (int x = 0; x <= MAXMAPX - 1; x++)
                {
                    for (int y = 0; y <= MAXMAPY - 1; y++)
                    {
                        mapTile[x, y].updateTile(0);
                    }
                }
            }
        }
    blah blah, all chipper. I think I've done all that correctly. Anyhow, now I'm trying to test out my objects and I have this in the main method

    Code:
    static void Main()
            {
    
                Map testMap = new Map();
    
                testMap.mapTile[1, 0].updateTile(0,1);
    
                Console.WriteLine(testMap.mapTile[0, 0].getTileLayer(0) + "\n" + testMap.mapTile[1, 0].getTileLayer(0));
            }
    Now, what I assume that does is find that tile using the index and calls that method passing that data by. However, when I print an unedited one, and the one I deliberately edited, the output I get is

    Code:
    1
    1
    I hope someone can understand why I'm stumped. I've been stuck for hours and googleing this hasn't been very easy, but thankfully, I was able to find this forum.

    I hope someone can offer guidance! Also, if there's anything I can do as far as optimization goes, that'd be great. I imagine I'm doing this in a very poor manner...

  2. #2
    Join Date
    Oct 2013
    Posts
    16

    Re: A little array issue

    Code:
            public struct Tile
            {
                private const int MAXLAYERS = 4;
                private const int MAXDATA = 5;
                private int tileAttribute;
                private int[] tileLayer;
                private object[] tileData;
    
                public void setLayer()
                {
                    tileLayer = new int[MAXLAYERS];
                }
    
                public void setData()
                {
                    tileData = new object[MAXDATA];
                }
    
                //this method is used to update the tile's graphic, given a specific layer
                public void updateTile(int layer, int tile)
                {
                    tileLayer[layer] = tile;
                }
    
                // overloaded updateTile, allowing you to update just the attribute
                public void updateTile(int attribute)
                {
                    tileAttribute = attribute;
                }
    
                //overloaded updateTile, allows you to update the tileData of a tile, given a dataIndex
                public void updateTile(int dataIndex, object newData)
                {
                    tileData[dataIndex] = newData;
                }
    
                public int getTileLayer(int layer)
                {
                    return tileLayer[layer];
                }
    
                public int getTileAttribute()
                {
                    return tileAttribute;
                }
    
                public object getTileData(int dataIndex)
                {
                    return tileData[dataIndex];
                }
    
                public int getMaxLayers()
                {
                    return MAXLAYERS;
                }
    
                public int getMaxData()
                {
                    return MAXDATA;
                }
            }
    
            public class Map
            {
                private const int MAXMAPX = 20;
                private const int MAXMAPY = 15;
    
                public Tile[,] mapTile = new Tile[MAXMAPX, MAXMAPY];
    
                // Messy blank map ctor
                public Map()
                {
                    // nested for loops to make all tiles blank
                    for (int x = 0; x <= MAXMAPX - 1; x++)
                    {
                        for (int y = 0; y <= MAXMAPY - 1; y++)
                        {
                            for (int z = 0; z <= mapTile[x, y].getMaxLayers() - 1; z++)
                            {
                                mapTile[x, y].setLayer();
                                mapTile[x, y].updateTile(z, 0);
                            }
                        }
                    }
    
                    // nested for loops to make all map data blank
                    for (int x = 0; x <= MAXMAPX - 1; x++)
                    {
                        for (int y = 0; y <= MAXMAPY - 1; y++)
                        {
                            for (int z = 0; z <= mapTile[x, y].getMaxData() - 1; z++)
                            {
                                mapTile[x, y].setData();
                                mapTile[x, y].updateTile(z, "");
                            }
                        }
                    }
    
                    // for loop to make all attributes blank
                    for (int x = 0; x <= MAXMAPX - 1; x++)
                    {
                        for (int y = 0; y <= MAXMAPY - 1; y++)
                        {
                            mapTile[x, y].updateTile(0);
                        }
                    }
                }
            }
    Your problem was the static keyword, you weren't using new vars for each element of mapTile but always the same.

    You can resolve it setting the new vars for each instance.
    Last edited by Reroto; November 7th, 2013 at 07:34 AM.

  3. #3
    Join Date
    Nov 2013
    Location
    Georgia
    Posts
    4

    Re: A little array issue

    Oh my! I thought using the static keyword made the property available before initialization; but I had a hunch it was the root of the cause. I have no idea why it does what it did, but your edits made it work + I understand them/they make sense.

    Thank you soo much! I really like this place.

    If you couldn't guess I'm kind of a new C# coder, but I've got lots of experience with VB6.0, so I'm trying to learn OOP and the best way I thought was to recode the game I was working on in C#.

    Anyhow, thanks a lot for the help! Have a good day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured