Jump to content

Inconsistent accessibility

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
kavery

kavery

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
I got a problem with one of my classes
the error is :
Error 1 Inconsistent accessibility: field type 'TS_Army_Sub.TSStats' is less accessible than field 'TS_Army_Sub.PlayerSub.stats' D:\Uni Work\GHAP\TS Army Sub V1.0\TS Army Sub\PlayerSub.cs 26 24 TS Army Sub
:cursing:

the class TSStats looks like:

using System;

using System.Collections.Generic;

using System.Text;


//kavery

//TSStats (Stats Attemp 2)   v1.0

//03-12-2009


namespace TS_Army_Sub

{

    class TSStats

    {

        private int mHealth;

        private int mScore;

        private int mAirSupply;

        public bool aboveWater;


        public int health

        {

            get { return mHealth; }

            set { mHealth = value; }

        }


        public int score

        {

            get { return mScore; }

            set { mScore = value; }

        }


        private int air

        {

            get { return mAirSupply; }

            set { mAirSupply = value; }

        }


        public TSStats()

        {

            mHealth = 250;

            mScore = 0;

            mmAirsupply = 20;

            aboveWater = true;

        }


        public bool healthCheck()

        {

            if (mHealth <= 0)

                return false;

            return true;

        }


        public void changeScore(int change)

        {

            mScore += change;

        }


    }

}

it's fine if i create it like

private TSStats stats;

but i get the error shown above with

public TSStats stats;

and i need it public so that i can ref the health ref for the class handling the gui hud in the game.

is there anything else that needs explaining?

can any tell me why this error appears. or give me a simple solutions,

PS. my original Stats Class had all varibles public and gave the exact same problem which is why i made this one.:confused:

#2
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Mark your class declaration as PUBLIC and it should work like a charm. I also modified your HealthCheck (bool) property, that produces the same result, just a line of code less. All the variables in your class should be private and if you want to modify or get a variable value, use public properties.

Also notice the public Score property, where the setter is coded as private: private set { m_score = value; } which means that this variable value can only be changed by class itself or by using your public method ChangeScore(int value) which in this case is much better and safer approach.

By Microsoft standards you should use CamelCase notation for naming your properties and methods. Properties and methods should always start with Uppercase.

If this class will be used in multi threaded application, you should also implement some thread safe logic when getting and setting the values.

This is how your class should be:

namespace TS_Army_Sub
{
    [B][U]public[/U][/B] class TSArmySub
    {
        public TSArmySub()
        {
            m_health = 250;
            m_score = 0;
            m_air_supply = 20;
            m_above_water = true;
        }


        public bool CheckHealth()
        {
            return this.HealthCheck;
        }


        public void ChangeScore(int value)
        {
            m_score += value;
        }


        public bool HealthCheck
        {
            get { return m_health >= 0; }
        }


        public int Health
        {
            get { return m_health; }
            set { m_health = value; }
        }
        

        public int Score
        {
            get { return m_score; }
            private set { m_score = value; }
        }
        

        public int AirSupply
        {
            get { return m_air_supply; }
            set { m_air_supply = value; }
        }


        public bool AboveWater
        {
            get { return m_above_water; }
            set { m_above_water = value; }
        }


        private int m_health;
        private int m_score;
        private int m_air_supply;
        private bool m_above_water;
    }
}

Edited by FlashM, 03 December 2009 - 11:32 AM.


#3
kavery

kavery

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
I've used your code a it works fine and thank you you the health check implementation for less lines.:thumbup:

it's a great help.

i am still un sure how the Inconsistent accessibility Error comes about in mine though, are there anyways i can avoid it in the future.:confused:

and i'll make note of your help in the file if that pleases ya

#4
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts

kavery said:

I've used your code a it works fine and thank you you the health check implementation for less lines.:thumbup:

it's a great help.

i am still un sure how the Inconsistent accessibility Error comes about in mine though, are there anyways i can avoid it in the future.:confused:

and i'll make note of your help in the file if that pleases ya

hehe, thanks :-)
Anyway the thing is that your class declaration started like so:

class MyNewClass
{

}

so before the class keyword there is nothing written (meaning no PRIVATE, PUBLIC,...). This means that your class is actually marked as INTERNAL as this is by default. The properties in your class were declared as PUBLIC:

public int MyProperty
{
get { ... }
set { ... }
}

Because of that, PUBLIC accessor has more accessibility than your classes default 'INTERNAL' accessor. That's why your code produced this error.

more...........................less accessible
PUBLIC > INTERNAL > PRIVATE (something like that)


I hope this answers your question.

#5
kavery

kavery

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
i see

thank very much
i hope i can be of some help to ya in the future my friend