Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Encapsulation and Arrays Ternary If

Compiler Error CS0622

After running the compiler I got this error, treehouse:~/workspace$ mcs -out:TreehouseDefense.exe *.cs
Game.cs(13,32): error CS0622: Can only use array initializer expressions to assign to array types. Try using a new expression instead
Compilation failed: 1 error(s), 0 warnings

here's my code,

 try
          {
            MapLocation path = {
              new MapLocation(0,2,map),
              new MapLocation(1,2,map),
              new MapLocation(2,2,map),
              new MapLocation(3,2,map),
              new MapLocation(4,2,map),
              new MapLocation(5,2,map),
              new MapLocation(6,2,map),
              new MapLocation(7,2,map)
            };
          }
          catch(OutOfBoundsException ex)
          {
            Console.WriteLine(ex.Message);
          }
          catch (TreehouseDefenseException)
          {
            Console.WriteLine("Unhandled TreehouseDefenseException");
          }
          catch(Exception)
          {
            Console.WriteLine("Unhandled Exception");
          }

1 Answer

Steven Parker
Steven Parker
229,732 Points

You probably intended to declare an array of "MapLocation" objects instead of just one:

            MapLocation[] path = {    // note that brackets [] indicate an array

Sweet that fixed it, thank you.