dodgrile
dodgrile

Reputation: 3189

Inconsistent Accessibility: Parameter type is less accessible than method

I'm trying to pass an object (a reference to the currently logged on user, basically) between two forms. At the moment, I have something along these lines in the login form:

private ACTInterface oActInterface;

public void button1_Click(object sender, EventArgs e)
    {
        oActInterface = new ACTInterface(@"\\actserver\Database\Premier.pad",this.textUser.Text,this.textPass.Text);

        if (oActInterface.checkLoggedIn())
        {
            //user has authed against ACT, so we can carry on
            clients oClientForm = new clients(oActInterface);
            this.Hide();
            oClientForm.Show();
        }
        else...

on the next form (clients), I have:

public partial class clients : Form
{
    private ACTInterface oActInt {get; set;}

    public clients(ACTInterface _oActInt)

...which results in me getting:

Error   1   Inconsistent accessibility: 
parameter type 'support.ACTInterface' is less accessible than method    
'support.clients.clients(support.ACTInterface)'  
c:\work\net\backup\support\support\clients.cs   20  16  support

I don't really understand what the problem is - both fields are private, and accessed by the relevant public method from the form. Googling doesn't really help, as it just points towards one element being public and the other private, which isn't the case here.

Anybody help?

Upvotes: 298

Views: 594078

Answers (14)

Ahsan Mirza
Ahsan Mirza

Reputation: 49

In my case i make the internal class to public where I am using the class as model or DTO in another class

Upvotes: 1

Mahdi nezam parast
Mahdi nezam parast

Reputation: 649

In my case I hadone class in a file and I was passing a instance of that class to the constructor of my form in another file. The problem was had declared the class without the public modifier : class MyClass {}

I could have solved it by changing it to public class MyClass {}

Upvotes: 4

Pavan Joshi
Pavan Joshi

Reputation: 61

All the answers that say make the type ActInterface as public are right. I am only putting this post to explicitly mention why that's an issue

If a parameter to your public class constructor is private or internal qualified class, it means you wont be able to create an object of that parameter class from outside of the assembly and when you cannot instantiate the parameter object, how can you call this constructor to instantiate an object of this class ?

Upvotes: 0

Harrish Selvarajah
Harrish Selvarajah

Reputation: 1873

parameter type 'support.ACTInterface' is less accessible than method    
'support.clients.clients(support.ACTInterface)' 

The error says 'support.ACTInterface' is less accessible because you have made the interface as private, at least make it internal or make it public.

Upvotes: 11

Sadeq Shajary
Sadeq Shajary

Reputation: 351

You can get Parameter (class that have less accessibility) as object then convert it to your class by as keyword.

Upvotes: 1

Keenan Stewart
Keenan Stewart

Reputation: 634

When I received this error, I had a "helper" class that I did not declare as public that caused this issue inside of the class that used the "helper" class. Making the "helper" class public solved this error, as in:

public ServiceClass { public ServiceClass(HelperClass _helper) { } }

public class HelperClass {} // Note the public HelperClass that solved my issue.

This may help someone else who encounters this.

Upvotes: 5

Yousri Mousa
Yousri Mousa

Reputation: 15

Try making your constructor private like this:

private Foo newClass = new Foo();

Upvotes: -5

Nisha
Nisha

Reputation: 1853

Make the class public.

class NewClass
{

}

is the same as:

internal class NewClass
{

}

so the class has to be public

Upvotes: 112

Simon Kiely
Simon Kiely

Reputation: 6050

After updating my entity framework model, I found this error infecting several files in my solution. I simply right clicked on my .edmx file and my TT file and click "Run Custom Tool" and that had me right again after a restart of Visual Studio 2012.

Upvotes: 0

ltsstar
ltsstar

Reputation: 892

If this error occurs when you want to use a classvariable in a new form, you should put the class definition in the

Formname.Designer.cs

instead of the Formname.cs file.

Upvotes: 0

Craig W.
Craig W.

Reputation: 18175

The problem doesn't seem to be with the variable but rather with the declaration of ACTInterface. Is ACTInterface declared as internal by any chance?

Upvotes: 4

Marc Gravell
Marc Gravell

Reputation: 1064004

If sounds like the type ACTInterface is not public, but is using the default accessibility of either internal (if it is top-level) or private (if it is nested in another type).

Giving the type the public modifier would fix it.

Another approach is to make both the type and the method internal, if that is your intent.

The issue is not the accessibility of the field (oActInterface), but rather of the type ACTInterface itself.

Upvotes: 33

James Gaunt
James Gaunt

Reputation: 14793

What is the accessibility of the type support.ACTInterface. The error suggests it is not public.

You cannot expose a public method signature where some of the parameter types of the signature are not public. It wouldn't be possible to call the method from outside since the caller couldn't construct the parameters required.

If you make support.ACTInterface public that will remove this error. Alternatively reduce the accessibility of the form method if possible.

Upvotes: 12

jason
jason

Reputation: 241769

Constructor of public class clients is public but it has a parameter of type ACTInterface that is private (it is nested in a class?). You can't do that. You need to make ACTInterface at least as accessible as clients.

Upvotes: 452

Related Questions