Accessing interfaces from classes that don't implement them?
I have an interface, IFindable
that is implemented by a few classes. One other World
class holds a List<IFindable> items;
I have set up a getItems method in my Worl开发者_如何学编程d
class, to return the list of IFindables. Now, I am trying to access that list from my Default.aspx.cs
class (this is a web project). Unfortunately, I don't seem to be able to since this class doesn't understand what IFindable is. I get the following error:
Inconsistent accessibility: return type
'System.Collections.Generic.List<IFindable>' is less accessible than
method 'World.getItems()'
Why is this? Have I gone about this wrong?
It sounds like your IFindable
interface isn't public, change it to this
public interface IFindable ...
If your current declaration looks like this
interface IFindable ...
Then the compiler is using the default accessibility which is internal
Interfaces, like classes, can be declared as public or internal types. Unlike classes, interfaces default to internal access. Interface members are always public, and no access modifiers can be applied. http://msdn.microsoft.com/en-us/library/ms173121%28VS.80%29.aspx
Have you defined your interface as public ?
public interface IFindable
{
...
}
Others have suggested making your interface public. An alternative is to make your getItems()
method internal:
internal List<IFindable> getItems()
{
...
}
(While you're at it, I suggest you either make it GetItems()
or a property called Items
, in order to follow .NET conventions. getItems()
is very Java-like.)
You might also need to put a using statement to the correct namespace at the top of your class file
精彩评论