Share via


why am i getting 'inaccessible due to its protection level'??

Question

Thursday, April 2, 2009 10:22 PM

why am i getting 'inaccessible due to its protection level'??
thats the only error i am getting when i try to compile the app
 

this is the first line in my Main(string[] args) method:
new Box(30.0f, 20.0f)

when i compile it, i get an error:
it tells me that "Box(float, float) is inaccessible due to its protection level"

So, what does Box look like?

here's the signature:
    class Box : IDimensions

by the way, if i make it
public class Box : IDimensions
i still get the same error, so thats not it

ok, how about the IDimensions interface, what's the sig on that?

looks like this:
    interface IDimensions

(again, if i have 'public' in both the IDimensions interface
and the class Box, i still get the same error, so that's not it)

 

where did i get the code?
i got it from

http://msdn.microsoft.com/en-us/library/44a9ty12.aspx

in fact, here it is in its entirety:

-the only diff is that I broke it out into separate .cs files:
I have an

IDimensions.cs file,
aBox.cs file
and the
Program.cs file

here's the code from msdn. . .

<<<<<
C# Copy Code
interface IDimensions
{
    float getLength();
    float getWidth();
}

class Box : IDimensions
{
    float lengthInches;
    float widthInches;

    Box(float length, float width)
    {
        lengthInches = length;
        widthInches = width;
    }
    // Explicit interface member implementation:
    float IDimensions.getLength()
    {
        return lengthInches;
    }
    // Explicit interface member implementation:
    float IDimensions.getWidth()
    {
        return widthInches;
    }

    static void Main()
    {
        // Declare a class instance box1:
        Box box1 = new Box(30.0f, 20.0f);

        // Declare an interface instance dimensions:
        IDimensions dimensions = (IDimensions)box1;

        // The following commented lines would produce compilation
        // errors because they try to access an explicitly implemented
        // interface member from a class instance:                  
        //System.Console.WriteLine("Length: {0}", box1.getlength());
        //System.Console.WriteLine("Width: {0}", box1.getwidth());

        // Print out the dimensions of the box by calling the methods
        // from an instance of the interface:
        System.Console.WriteLine("Length: {0}", dimensions.getLength());
        System.Console.WriteLine("Width: {0}", dimensions.getWidth());
    }
}

>>>>>

All replies (2)

Thursday, April 2, 2009 11:06 PM ✅Answered | 1 vote

You've got a private constructor. Make it public, and your code will compile.Visit my blog: http://www.cuttingedge.it/blogs/steven/


Friday, April 3, 2009 12:40 AM ✅Answered

wowwww.  i just did it and it worked!!!   i did not realize if you  dont specify an accessor then it defaults to private.

thank you