Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inconcictent Accessibility - Don't know why 1

Status
Not open for further replies.

MadJock

Programmer
May 25, 2001
318
GB
Hi all,

I have three classes, all within the same assembly and namespace. When I compile I get:

"Inconsistent accessibility: parameter type 'Foo' is less accessible than method MyClass.VirtualMethod(Foo)"
"Inconsistent accessibility: parameter type 'Foo' is less accessible than method MySubClass.VirtualMethod(Foo)"

I don't understand why!

Code sample below will produce error:
Code:
public class MyClass
{
	public static void Main()
	{
	}
	
	protected internal virtual void VirtualMethod(Foo foo)
	{
		// nothing here
	}
}

public class MySubClass: MyClass
{
	protected internal override void VirtualMethod(Foo foo){
		//	overridden method
 	}
}

class Foo
{
 	public string myString;
}

To my mind, the lack of modifier on class Foo makes it internal, therefor I don't see the problem. I can fix the compilation error by making the virtual method 'internal' rather than 'protected internal' but would like to understand why I get the compilation error.

Any explanation greatly appreciated!

Thanks,

Graeme


"Just beacuse you're paranoid, don't mean they're not after you
 
To my mind, the lack of modifier on class Foo makes it internal,
the default modifier is private, not internal.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks Jason,

If the class Foo is private and not contained within any other class, why is it visible at all? I can create a local variable within MyClass.VirtualMethod of type Foo.

Thanks,

Graeme


"Just beacuse you're paranoid, don't mean they're not after you
 
I'm wrong the default for a class is not private
msdn <F1> said:
The access levels protected and private are only allowed on nested classes.
...
The following five accessibility levels can be specified using the access modifiers:

public: Access is not restricted.

protected: Access is limited to the containing class or types derived from the containing class.

Internal: Access is limited to the current assembly.

protected internal: Access is limited to the current assembly or types derived from the containing class.

private: Access is limited to the containing type.
i take this to mean that protected internal means protected or internal, not protected and internal. because MyClass is public it could be overridden in another assembly. In that instance the other assembly needs access to Foo, so the modifer must change. If I have this correct you have a couple options:
1. make MyClass sealed
2. change the accessor of MyClass
3. change the accessor of Foo
4. change the accessor of VirtualMethod

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I'd mistakenly assumed (that word!) that protected internal meant protected AND internal... Thanks for your explanation.

"Just beacuse you're paranoid, don't mean they're not after you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top