Search code examples
c#linuxclassmonodevelop

MonoDevelop C# - Error while compiling class file


I am trying to compile a basic test class in MonoDevelop version 5.0.1.1. See the following code:

using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;

namespace testproject
{
    public class TestClass
    {
        public TestClass
        {
            string name;
            public string Name
            {
                get {return name;}
                set {name = value;}
            }
            int[] integers;
            public int this[int i]
            {
                get {if (i < 5) {return integers[i];} else {return -1;}}
                set {if (i < 5) {integers[i] = value;}}
            }
            public TestClass(string _name)
            {
                name = _name;
            }
            public override string ToString ()
            {
                string output = name + ":";
                for (int i = 0; i < 5; i++)
                {
                    if (i != 4) {output += " " + i.ToString() + ",";}
                    else {output += " " + i.ToString();}
                }
                return output;
            }
        }
    }
}

It is a very basic test class that I created to try out MonoDevelop on Linux (I just switched from VS2017 on Windows). When I try to compile, I get the following error(s):

/home/main/mono-cs/projects/test-project/test- project/TestClass.cs(3,3): Error CS1519: Invalid token '{' in class, struct, or interface member declaration (CS1519) (test-project)

and

/home/main/mono-cs/projects/test-project/test- project/TestClass.cs(1,1): Error CS1022: Type or namespace definition, or end-of-file expected (CS1022) (test-project)

I double checked, and all curly braces have proper corresponding ones. Any insight?

Thanks all!


Solution

    1. Remove public TestClass ,Which is Repeat adding in nest TestClass
    2. initialize integers otherwise you'll get a runtime error.(thank for @Ron Beyer remind)

    Here

    namespace testproject
    {
        public class TestClass
        {
            string name;
            public string Name
            {
                get {return name;}
                set {name = value;}
            }
            int[] integers;
            public int this[int i]
            {
                get {if (i < 5) {return integers[i];} else {return -1;}}
                set {if (i < 5) {integers[i] = value;}}
            }
            public TestClass(string _name)
            {
                name = _name;
                integers = new int[100];
            }
            public override string ToString ()
            {
                string output = name + ":";
                for (int i = 0; i < 5; i++)
                {
                    if (i != 4) {output += " " + i.ToString() + ",";}
                    else {output += " " + i.ToString();}
                }
                return output;
            }
    
        }
    }