VB Magic


Eeek, my first C# post! Multi-Dimensional Arrays in Micro Framework failure

Filed under: .NET, .Net Micro Framework, C#, Learning — Tags: , — vbmagic @ 1:42 pm


As I’m using the Gadgeteer/.Net Micro Framework, I’ve been forced to learn some more c# ;-).

I came across an issue where Multi-Dimensional arrays are not compatible with the micro framework.

Thanks to the help of Architect from the TinyCLR.com forums (See his blog link “Break Continue” in the blogroll to the right of the post), I’ve now been introduced to jagged arrays in C# which solved the issue.

This failed:

private char[,] maze = new char[20,20];

So I now use the following code to achieve the same thing:

 private char[][] maze = new char[20][];
// Stuff
        private void InitialiseMaze()
            for (int lp = 0; lp <= 19; lp++)
                maze[lp] = new char[20];

To see it in use, Here is a section of code used to populate the array from the code:

// Stuff
        private void LoadMaze()
            // read in the text file and convert to a string.
            string path = _mazePath + @"\store\" + _currentLevel + ".map";
            byte[] rawBytes = _storageDev.ReadFile(path);
            char[] rawCharacters = System.Text.UTF8Encoding.UTF8.GetChars(rawBytes);
            string tmpMap = new string(rawCharacters);
            // Now split the text file into lines using the \r character from the text file
            // note this will leave the \n character at the end of each line
            string[] lines = tmpMap.Split('\r');
            // Get the header, removing the \n
            string header = lines[0].Trim('\n');
            // check maze version and the dimensions of the maze
            string[] bits = header.Split('|');
            string ver = bits[0];
            int width = int.Parse(bits[1]);
            int height = int.Parse(bits[2]);
            if (ver != "1.0")
                throw new Exception("Maze version is incorrect");
            for (int y = 1; y <= height; y++)
                char[] row = lines[y].Trim('\n').ToCharArray();
                for (int x = 0; x < width; x++)
                    maze[x][y-1] = row[x];

1 Comment »

  1. multidimensional array examples….c# multidimensional array


    Comment by williamholdin — 2014/04/24 @ 8:29 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: