Jump to content
  • Advertisement
Sign in to follow this  
dxFoo

[.net] Inconsistent Accessibility

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Error	1	
Inconsistent accessibility: parameter type 'MyGame.Player' is less accessible than method 'MyGame.GameForm.GameForm(MyGame.Player)
What would cause this? Player is derived from class Common. In the init form, I'd pass it to the game form witn this..
    public partial class Form1 : Form
    {
        Player PC = new Player();
        GameForm gameForm = new GameForm(PC);
    }
Gameform:
    public partial class GameForm : Form
    {
        Player PC = new Player();

        public GameForm(Player PC)
        {
            this.PC = PC;
            InitializeComponent();
        }
    }
They should be private by default, so what's inconsistent of it?

Share this post


Link to post
Share on other sites
Advertisement
How is MyGame.Player defined? Since MyGame.GameForm.GameForm is public, the definition for MyGame.Player also needs to be public, meaning it needs to look like this:


namespace MyGame
{
public Player /* note the public */
{
// ...
}
}



Otherwise if Player is less than public, other people can access the public GameForm constructor, but they'd never be able to pass it a Player object because it would be inaccessible by them.

Share this post


Link to post
Share on other sites
It's currently like this..


/// <summary>
/// Player Class
/// </summary>
class Player : Common
{
public Player()
{
}
}

Share this post


Link to post
Share on other sites
A class without access modifiers defaults to internal accessibility, which means it's only visible to the assembly (basically the DLL or EXE) in which it is defined. You'll need to change your code to public Player, or change the GameForm constructor to internal GameForm(Player PC).

Short summary of accessibility levels.

Share this post


Link to post
Share on other sites
Internal worked :) I know I've had this issue before, but I never used internal to solve it. I guess it depends on how the accessibility is set up?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When u declare a class is good practice to always include ur accessibility, ex:

public class MyClass
{
public int MyInterger;
}

here the "public" is the access command... if u don't classes default to internal when declared outside any other classes else they will be "private" as well as any fields or properties defined without access command.

Now, if u don't want people messing with ur code, u should always think about accessibility, and how it will make it easy for u and not unwanted hackers to change values in your classes.

Don't know how old this thread is..lol... but I could not resist on posting...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!