Hey,

I'm currently doing some work on a program i'm creating for Uni, now i'm getting the above error on the following line of code:

if ((position.X < boundary.Width - 50) && (counter == 0))

All the variables are set to Public. The error is within an 'Alien' class but the error message says this: ".enemy.counter is inaccessible due to protection levels"

Visual Basic 2008 is locating the error at the 'counter'. I can upload more code if required.

Any help would be really appreciated!!!

Cheers

P

Recommended Answers

All 7 Replies

Please paste your class definition and the method the error occurs

This is where one of the errors is:

public override void Move()
        {
            if (Frame > Speed)
            {
                if ((position.X < boundary.Width - 50) && (counter == 0))
                {
                    position.X += 1;
                    picture.Location = position;
                }
                else if ((position.X == boundary.Width - 50) && (counter == 0))
                {
                    position.Y = position.Y + 50;
                    counter++;
                    picture.Location = position;
                }
                else if ((position.X > 0) && (counter == 1))
                {
                    position.X -= 1;
                    picture.Location = position;
                }
                else if ((position.X == 0) && (counter == 1))
                {
                    position.Y = position.Y + picture.Height;
                    counter --;
                    picture.Location = position;
                }

                Frame = 0;
            }
            else
            {
                Frame++;
            }
        }

Please include the class definition of where the method is.

I mean the part where you define all the variables in the class and the class name itself etc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Media;

namespace SpaceInvaders
{
    class Alien:Enemy
    {
        public Alien(PictureBox picture, Panel boundary) : base(picture, boundary)
        {

        }

        public override void Die()
        {

            base.Die();
        }


        public override void Move()
        {
            if (Frame > Speed)
            {
                if ((position.X < boundary.Width - 50) && (counter == 0))
                {
                    position.X += 1;
                    picture.Location = position;
                }
                else if ((position.X == boundary.Width - 50) && (counter == 0))
                {
                    position.Y = position.Y + 50;
                    counter++;
                    picture.Location = position;
                }
                else if ((position.X > 0) && (counter == 1))
                {
                    position.X -= 1;
                    picture.Location = position;
                }
                else if ((position.X == 0) && (counter == 1))
                {
                    position.Y = position.Y + picture.Height;
                    counter --;
                    picture.Location = position;
                }

                Frame = 0;
            }
            else
            {
                Frame++;
            }
        }

    }

}

Please make sure that the Enemy class contains the variable "counter" and that it isn't set to private. It must be set to either "protected" or "public".

The enemy class contains the counter variable like this:

class Enemy
    {
        public bool active;


        private int frame = 1;           //speed is set to slow, if the frame is 1 and speed is 100 this is set to stopped, and frame is 1 and speed is -1 this is faster

        public int Frame
        {
            get { return frame; }
            set { frame = value; }
        }
        private int speed = 0;

        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }
        public Panel boundary;
        public PictureBox picture;
        public Point position;
        int counter;

Along with more code, however when i change the int to public I get the following error:

invalid token ';' in class struct or interface member declaration

As you've left no specific accessor there, it defaults to private. You need to redefine it as protected int counter; This will allow the Enemy class and any parent class that inherits it, but not any other class, to access it.

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.