简体   繁体   中英

C# - is inaccessible due to its protection level

I can't figure out why this is the case. I'm creating a View class from Game class and then I'm trying to call a method from View in Game and send it the entire game object as a parameter. If I send single variables as a parameter then it work's no problem but I wanted to send the whole Game object so that only one thing would be passed.

Game class

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Airfield
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        // Device Objects
        GraphicsDevice device = null;
        GraphicsDeviceManager graphics = null;
        MouseState mouse;

        // Game Objects
        SpriteBatch spriteBatch;
        Texture2D b = null;
        View view = null;

        // Arrays
        Buoy [] buoy = new Buoy[3];
        Plane [] plane = new Plane[3];

        // Variables
        bool selected = false;
        int index = 0;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
        }

        protected override void Initialize()
        {
            Content.RootDirectory = "Content"; 
            this.IsMouseVisible = true;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.ApplyChanges();
            base.Initialize();

            view = new View();

            for (index = 0; index < buoy.Length; index++)
            {
                buoy[index] = new Buoy();
                plane[index] = buoy[index].CreatePlane();
            }
        }

        protected override void LoadContent()
        {
            device = graphics.GraphicsDevice;
            spriteBatch = new SpriteBatch(device);
            b = Content.Load<Texture2D>("buoy");
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            mouse = Mouse.GetState();

            if (selected == true || mouse.X >= buoy[0].position.X - 3 && mouse.X <= buoy[0].position.X + 19 && mouse.Y >= buoy[0].position.Y - 3 && mouse.Y <= buoy[0].position.Y + 19)
            {
                if (mouse.LeftButton == ButtonState.Pressed)
                {
                    selected = true;
                    buoy[0].position= new Vector2(mouse.X - 8, mouse.Y - 8);
                }

                else
                {
                    selected = false;
                }
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            view.DrawScreen(this);
        }
    }
}

View Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Airfield
{
    class View
    {
        public View()
        {

        }

        public void DrawScreen(Game game)
        {
            game.device.Clear(Color.CornflowerBlue);

            game.spriteBatch.Begin();
            DrawBuoys(game);
            game.spriteBatch.End();
        }

        public void DrawBuoys(Game game)
        {
            for (int x = 0; x < game.buoy.Length; x++)
            {
                game.buoy[x].DrawBuoy(game.spriteBatch, game.b);
            }
        }
    }
}

Basically I get the error for every time that I try and use something that has come from Game.

The problem here is that all of the members inside of Game are marked as protected or not marked at all which defaults to private . This means that only Game and any classes which derive from it will have access to those protected members and no-one else to private . The type View is a completely separate type and hence can't access any members that are protected or private .

To expose the private fields they need to be marked as internal or public .

public class Game : Microsoft.Xna.Framework.Game
{
  ...
  public SpriteBatch spriteBatch;
}

Most of these members are also labeled as override hence you are locked into protected . But you can use another non-protected member to call into the protected . It's not clear from the sample if you want to invoke these methods or not.

All of this is implicitly private :

// Device Objects
GraphicsDevice device = null;
GraphicsDeviceManager graphics = null;
MouseState mouse;

// Game Objects
SpriteBatch spriteBatch;
Texture2D b = null;
View view = null;

// Arrays
Buoy [] buoy = new Buoy[3];
Plane [] plane = new Plane[3];

// Variables
bool selected = false;
int index = 0;

You'd need to change them to public , which is a bad idea for member variables. Or you can create properties to access the member variables:

public SpriteBatch SpriteBatch {
    get { return this.spriteBatch; }
    protected set { this.spriteBatch = value; }
}

By default in C# any fields that do not have accessibility explicitly defined are assumed to be private. So, you will need to do the following in your Game class:

    // Device Objects
    public GraphicsDevice device = null;
    public GraphicsDeviceManager graphics = null;
    public MouseState mouse;

    // etc... for your other fields

Second, it's worth noting that all of your Game class's methods have protected accessibility, which means that only classes that inherit from Game will be able to call those methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM