views:

110

answers:

4

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 World 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?

+8  A: 

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

Bob
Forgot that C# defaulted to private. Thanks!
4501
C# defaults to "the most private available in the context" - which is `internal` for top-level types and `private` for members within a type.
Jon Skeet
+3  A: 

Have you defined your interface as public ?

public interface IFindable 
{
...
}
Mongus Pong
My code example is more complete! :-)
Mongus Pong
A: 

You might also need to put a using statement to the correct namespace at the top of your class file

Joel Martinez
No, the compiler clearly knows which type is involved, otherwise it would have complained about that. It can't tell the visibility unless it knows the type.
Jon Skeet
+3  A: 

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.)

Jon Skeet
+1 for not assuming too much about the intentions of the poster.
Jeff Sternal
Nice suggestion. The habits you develop growing up with Java..
4501