Member Avatar for Moirae

Hello, I've written a class, and I've created an instance. When I try to debug it gives me an error and I really don't understand what that means. I looked at some other web sites but I don't understand what do I have to do. Can somebody please help me?
This is the class code:

namespace WindowsApplication1
{
    public class Field
    {
        private bool[,] matrix = new bool[Const.ROW, Const.COLUMN];

        public Field()
        {
            initializeField();
        }

        public void initializeField()
        {
            bool temp = true;
            for (int i = 0; i < Const.ROW; i++)
                for (int j = 0; j < Const.COLUMN; j++)
                {
                    if (i > 0)
                        temp = !matrix[i - 1, j];
                    matrix[i - 1, j] = new bool(temp);

                    temp = !temp;
                }
        }
        public bool[,] getField()
        {
            return matrix;
        }

        public void setElement(int i, int j)
        {
            matrix[i, j] = !matrix[i, j];
            try
            {
                matrix[i - 1, j] = !matrix[i - 1, j];
            }
            catch (IndexOutOfRangeException)
            { }
            try
            {
                matrix[i + 1, j] = !matrix[i + 1, j];
            }
            catch (IndexOutOfRangeException)
            { }
            try
            {
                matrix[i, j - 1] = !matrix[i, j - 1];
            }
            catch (IndexOutOfRangeException)
            { }
            try
            {
                matrix[i, j + 1] = !matrix[i, j + 1];
            }
            catch (IndexOutOfRangeException)
            { }
        }

        public Boolean getElement(int i, int j)
        {
            return matrix[i, j];

        }
    }
}

Const is a class that I use to store some constant values e.g. size of rows and columns, so I can modify that values easily later.
Field class is instantiated in the form class (Windows Application Form).
Thanks

Can you post the code where you instantiate an object of Field class?

Member Avatar for Moirae

Sure thing. Btw error is in line 20.
This code is a bit bigger, and all the variables are written in my language so if skipped some translations please tell me. It's a bit hard for me to write all variable names in English.

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        int clickedNum, emptyNum, coloredNum;
        Field fld = new Field();
        Button[,] btnArr = new Button[Const.ROW, Const.COLUMN];
        Button reset;
        Label lClickedNum, lEmptyNum, lColoredNum;

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            for (int i = 0; i < Const.ROW; i++)
            {
                for (int j = 0; j < Const.COLUMN; j++)
                {
                    //b = new Button();
                    //b.Size = new Size(75, 75);
                    //b.Text = "Button" + ((i + 1) * (j + 1));
                    //btnArr[i, j] = b;
                    btnArr[i, j] = new Button();
                    btnArr[i, j].Size = new Size(75, 75);
                    btnArr[i, j].Margin = new Padding(0, 0, 0, 0);

                    btnArr[i, j].Text = "Button" + ((i + 1) * (j + 1));


                    if (fld.getElement(i, j) == false)
                        btnArr[i, j].BackColor = Color.AliceBlue;
                    else
                        btnArr[i, j].BackColor = Color.AntiqueWhite;
                    //Controls.Add(btnArr[i, j]);
                    //InitializeComponent();
                }
            }
            
            reset = new Button();
            reset.Size = new Size(380, 30);
            reset.Margin = new Padding(0, 0, 0, 0);
            reset.Text = "Resetiraj";
            reset.TextAlign = ContentAlignment.MiddleCenter;
            Controls.Add(reset);
            
            lClickedNum = new Label();
            lClickedNum.Size = new Size(10, 25);
            lClickedNum.Margin = new Padding(0, 0, 0, 0);
            lClickedNum.Enabled = false;
            Controls.Add(lClickedNum);
            
            lColoredNum = new Label();
            lColoredNum.Size = new Size(10, 25); 
            lColoredNum.Margin = new Padding(0, 0, 0, 0);
            lColoredNum.Enabled = false;
            Controls.Add(lColoredNum);
            
            lEmptyNum = new Label();
            lEmptyNum.Size = new Size(10, 25);
            lEmptyNum.Margin = new Padding(0, 0, 0, 0);
            lEmptyNum.Enabled = false;
            Controls.Add(lEmptyNum);
        }

        private void Button_MouseDown(object sender, MouseEventArgs e)
        {
            clickedNum++;
            if (e.Button.Equals(reset))
            {
                clickedNum = 0;
                fld.initializeField();
            }
            for (int i = 0; i < Const.ROW; i++)
                for (int j = 0; j < Const.COLUMN; j++)
                {
                    if (e.Button.Equals(btnArr[i, j]))
                        fld.setElement(i, j);
                }
            refreshField();

            for (int i = 0; i < Const.ROW; i++)
                for (int j = 0; j < Const.COLUMN; j++)
                {
                    if (btnArr[i, j].BackColor == Color.AntiqueWhite)
                        coloredNum++;
                    else
                        emptyNum++;
                }
            lClickedNum.Text = "Broj klikova: " + clickedNum;
            lColoredNum.Text = "Broj praznih polja: " + coloredNum;
            lEmptyNum.Text = "Broj punih polja: " + emptyNum;

            if ((emptyNum == 0) || (coloredNum == 0))
                pobjeda();

            emptyNum = 0;
            coloredNum = 0;
        }

        private void win()
        {
            reset.Text = "POBJEDA!!!";
            fld.initializeField();
            refreshField();
         }
        

        private void refreshField()
        {
            for (int i = 0; i < Const.ROW; i++)
                for (int j = 0; j < Const.COLUMN; j++)
                {
                    if (fld.getElement(i, j) == false)
                        btnArr[i, j].BackColor = Color.AliceBlue;

                    else
                        btnArr[i, j].BackColor = Color.AntiqueWhite;
                }
        }

    }
}

I left out the comments because it would take me too long to translate it.

What is the classname that is listed in the error message?

Type classname has no constructors defined error

is the error message number CS0143?

Upload your project. You're not posting the relevant information needed to solve the issue you're having.

Member Avatar for Moirae

error CS0143: The type 'bool' has no constructors defined
It points to Field class line

matrix[i - 1, j] = new bool(temp);

@sknake it will take me a while to translate the var and class names to english, is it ok if i just comment it??

You don't construct bools. You set them true or false:

matrix[i - 1, j] = true;
//or
matrix[i - 1, j] = false;
commented: quite right! +12

You are trying to call a constructor that doesn't exist. Try changing:

matrix[i - 1, j] = new bool(temp);

to

matrix[i - 1, j] = //True or false
Member Avatar for Moirae

Yes, I already tried that, and it gives me two errors

error CS0029: Cannot implicitly convert type 'bool' to 'int'
error CS0029: Cannot implicitly convert type 'bool[]' to 'bool'

If I write it like this instead:

if (temp == true)
     matrix[i - 1, j] = true;
else
      matrix[i - 1, j] = false;

Upload your project, don't worry about translating. You have something else going on in your application.

private bool[,] matrix = new bool[12, 41];
    private void button4_Click(object sender, EventArgs e)
    {
      var i1 = matrix[1, 2];
      matrix[1, 2] = true;
    }

That compiles for me

That project builds OK for me. Weird. Right click on your solution and press "Clean Solution" then right click and select "Rebuild Solution" to see if that clears it up.

Try downloading your project and running it to make sure you uploaded the right project.

Works fine here too...

Member Avatar for Moirae

This is horrible... That worked but app still crashes. I'm sure that I uploaded the right one... Thanks a lot for your help

You're welcome :)

Please mark this thread as solved as you have the project compiling and feel free to start another thread or ask another question regarding your application crash!

Member Avatar for Moirae

Thanks again

Quoting my dear friend serkan, "please mark as solved" ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.