Question

I have a class Computer.cs, which belongs to the form Computer, and I have an independent class Indicators.

In my code I'm getting an error:

Incompatibility accessibility: accessibility return type "WF.Code.Indicators"
method is below than accessibility return type "WF.Computer.ShowForm ()"

What is that means?

Computer.cs

namespace WF
{
    public partial class Computer : Form
    {
        Code.Indicators indicators = new Code.Indicators();

        public Computer()
        {
            if (indicators.isComputerAlreadyRunning == false)
            {
                InitializeComponent();
                indicators.isComputerAlreadyRunning = true;
            }
        }

        public Code.Indicators ShowForm() // Error
        {
            return new Code.Indicators(indicators.isComputerAlreadyRunning);
        }
    }
}

Indicators.cs

namespace WF.Code
{
    class Indicators
    {
        public Indicators(bool isComputerAlreadyRunning)
        {
            this.isComputerAlreadyRunning = isComputerAlreadyRunning;
        }

        public bool isComputerAlreadyRunning = false;
    }
}
Was it helpful?

Solution

Your method:

    public Code.Indicators ShowForm() // Error
    {
        return new Code.Indicators(indicators.isComputerAlreadyRunning);
    }

It returns an Indicators object and is of public visibility. However the type Indicators itself is not public, it's internal (by default, since you did not specify it ; see this answer for more information).

Declare the class Indicators as public to solve the issue, or set the method ShowForm as internal.

Edit:

To better explain why the compiler complains, imagine your code is actually compiled to a library. Somebody includes this library from another assembly to use it, and this somebody is going to call this public method ShowForm(because he can!).

He is going to get a reference pointing to an Indicators but from his point of view (actually from his assembly's point of view), he does not know what the class Indicators is, because its visibility is internal (as I said, by default). internal elements are not exposed to other assemblies on the contrary of public elements. This creates an incoherence and that it the reason why the compiler complains.

OTHER TIPS

It means that since ShowForm is public the return type must also be public.

you Indicators class is now :

namespace WF.Code
{
    class Indicators
    {
        public Indicators(bool isComputerAlreadyRunning)
        {
            this.isComputerAlreadyRunning = isComputerAlreadyRunning;
        }

        public bool isComputerAlreadyRunning = false;
    }
}

but should be :

namespace WF.Code
{
   public class Indicators
    {
        public Indicators(bool isComputerAlreadyRunning)
        {
            this.isComputerAlreadyRunning = isComputerAlreadyRunning;
        }

        public bool isComputerAlreadyRunning = false;
    }
}

in .NET if you instantiate a class outside the class' area then either should be internal or public code access.. otherwise such like you created class as private ( if there is no code access implementation as your implementation of Indicators - class Indicators- compiler accept that as private and you can't access out of the class)

Even if it was internal code access and you refer it as public you will get the same exception..

when your class' code access is public then should be all instances / inside methods or calls should be public, if it is internal then can be internal or private or protected

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top