Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I defined two class : 1- robi 2- police .in this thread program police should catch robber.
in Form1.cs I defined array of police object and robi object .compiler say me :

Error 1:
Inconsistent accessibility: field type 'WindowsFormsApplication1.police[]' is less accessible than field 'WindowsFormsApplication1.Form1.pol'

Error 2:
Inconsistent accessibility: field type 'WindowsFormsApplication1.robi[]' is less accessible than field 'WindowsFormsApplication1.Form1.rob'

All thing in form1.cs[design] is public.

this in my code:

police class:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class police
    {
        public int number;
        public int x;
        public int y;
     
    
        public police(int X, int Y, int num)
        {
            var form = Form.ActiveForm as Form1;
            x = X;
            y = Y;
            number = num;      
        }

        public void Move()
        {
            bool bol = true;
            int target = 0;
            Random rand = new Random();
            var form = Form.ActiveForm as Form1;
            for (int i = 0; i < Convert.ToInt32(form.textBox2.Text); i++)
            {
                bol = bol && form.rob[i].status;
            }
            while(bol == true)
            {
                do
                {
                    target = rand.Next(0,Convert.ToInt32(form.textBox2.Text));
                }
                while (form.rob[target].status == false);
                while ( true)
                {
                    if (Math.Abs(x - form.rob[target].x) < 3 && Math.Abs(y - form.rob[target].y) < 3)
                    {
                        form.rob[target].status = false;
                        break;
                    }
                    if (x < form.rob[target].x)
                    {
                        x++;
                    }
                    if (x > form.rob[target].x)
                    {
                        x--;
                    }
                    if (y < form.rob[target].y)
                    {
                        y++;
                    }
                    if (y > form.rob[target].y)
                    {
                        y--;
                    }
                }
                for (int i = 0; i < Convert.ToInt32(form.textBox2.Text); i++)
                {
                    bol = bol && form.rob[i].status;
                }
            }
        }
    }
}


robi class:

<pre lang="c#">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;


namespace WindowsFormsApplication1
{
    class robi
    {
        public int x;
        public int y;
        public int number;
        public bool status;

        public robi(int X, int Y, int num)
        {
            status = true;
            x = X;
            y = Y;
            number = num;
        }
        public void Run()
        {

        }
    }
}



Form1.cs

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;



namespace WindowsFormsApplication1
{
     
    public partial class Form1 : Form
    {
        public string[,] map;
        public police []pol;
        public robi[] rob;
        Bitmap map_bit;
        Graphics Map;
        Pen policepen;
        Pen robipen;
        Thread showmap;
        Thread[] policeman;
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            map = new string[pictureBox1.Size.Width, pictureBox1.Size.Height];
            map_bit = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
            Map = Graphics.FromImage(map_bit);
            policepen = new Pen(Color.Blue);
            robipen = new Pen(Color.Red);
            for (int i = 0; i < pictureBox1.Size.Width; i++)
            {
                for (int j = 0; j < pictureBox1.Size.Height; j++)
                {
                    map[i, j] = "  ";// :)
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Point coordinate = new Point();
            Random rand = new Random();
            //init array of police
            pol = new police[Convert.ToInt32(textBox1.Text)];
            for (int i = 0; i < Convert.ToInt32(textBox1.Text); i++)
            {
                coordinate.X = rand.Next(0, 400);
                coordinate.Y = rand.Next(0, 400);
                pol[i] = new police(coordinate.X, coordinate.Y,i);
                map[coordinate.X, coordinate.Y] = "P" + Convert.ToString(pol[i].number);
                  
            }
            //init array of rob
            rob = new robi[Convert.ToInt32(textBox2.Text)];
            for (int i = 0; i < Convert.ToInt32(textBox2.Text); i++)
            {
                coordinate.X = rand.Next(0, 400);
                coordinate.Y = rand.Next(0, 400);
                rob[i] = new robi(coordinate.X, coordinate.Y, i);
                map[coordinate.X, coordinate.Y] = "R" + Convert.ToString(rob[i].number);
            }
            RUN();
        }

        public void convertMapToImg()
        {
            string temp = "";
            char ch;
            bool bol = true;
            int p;
            for (int i = 0; i < Convert.ToInt32(textBox2.Text); i++)
			{
                bol = bol && rob[i].status;
			}
            while(bol)
            {
                for (int i = 0; i < pictureBox1.Size.Width; i++)
                {
                   for (int j = 0; j < pictureBox1.Size.Height; j++)
                   {
                       temp = map[i, j]; 
                       ch = temp[0];
                       if (ch == 'P')
                       {
                           ch = temp[1];
                           p = ch - 48;
                           Map.DrawEllipse(policepen, pol[p].x, pol[p].y,10,10);
                       }
                       ch = temp[0];
                       if (ch == 'R')
                       {
                           ch = temp[1];
                           p = ch - 48;
                           Map.DrawEllipse(robipen, rob[p].x, rob[p].y, 10, 10);
                       }
                    }
                }
                pictureBox1.Image = map_bit;
                for (int i = 0; i < Convert.ToInt32(textBox2.Text); i++)
                {
                    bol = bol && rob[i].status;
                }
            }
        }

        public void RUN()
        {
            showmap = new Thread(this.convertMapToImg);
            showmap.Start();
            policeman = new Thread[Convert.ToInt32(textBox1.Text)];
            for (int i = 0; i <Convert.ToInt32(textBox1.Text) ; i++)
            {
                policeman[i] = new Thread(pol[i].Move);
                policeman[i].Start();
            }
        }
    }
}
Posted
Comments
Philippe Mori 17-Dec-13 19:40pm    
Too much useless code for the question...

And by the way, it is usually possible to find help on most errors reported by the compiler sometime with examples.
Sergey Alexandrovich Kryukov 17-Dec-13 22:27pm    
Access levels should be consistent. Read about access modifiers and default access modifiers to learn what is assumed by default and the meaning of different levels of access. If still in doubt, show what lines of code cause the error messages, exactly.
As to the code samples, here are some directions for you: http://www.sscce.org.

And yes, you formally accepted a totally wrong answer. Please see my comment to "Solution 1". You need to validate the answers before accepting.

—SA

1 solution

Police and Robi classes are Private Internal. If you don't specify public on classes it is by default Private Internal.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Dec-13 22:23pm    
You lie. Have some shame. These classes cannot be private by definition. If they were private, no one would be able to access them, as they are on the top level. Syntax would not allow to declare the private. They are internal.

It's really bad to give an advice on some topic you have no clue on. Not answering is much better in such cases. Don't do evil.

—SA
bowlturner 18-Dec-13 9:04am    
You're a jerk. Lying implies intent to deceive. So I forgot it's internal vs. private, the problem was they weren't public

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900