Reputation: 19
Visual Studio gives me this error and cannot proceed.
The compiler reports that IArea
is less accessible as processingArea
. What can I do?
How do I then enter the parameter in the method processingArea()
?
I enter into an object that implements the IArea
, but how do I create an object that implements the interface?
public class GeometricShapes
{
double Area;
double @base;
double height;
public GeometricShapes()
{
}
public void processingArea(IArea poligono)
{
var area = poligono.CalculateArea();
Console.WriteLine(area);
Console.ReadLine();
}
}
public class Quadrato : GeometricShapes, IArea
{
double @base;
double height;
public double CalculateArea()
{
Random x = new Random();
this.@base = x.Next(100);
this.height = x.Next(100);
var Area = @base * @base;
return Area;
}
}
public class Rettangolo : GeometricShapes, IArea
{
double @base;
double height;
public double CalculateArea()
{
Random x = new Random();
this.@base = x.Next(100);
this.height = x.Next(100);
var Area = @base * height;
return Area;
}
}
interface IArea
{
double CalculateArea();
}
I made other errors?
Upvotes: 0
Views: 107
Reputation: 11480
In order for your interface to correctly work, you should ensure that it is marked public
. As for the parameter issue, you should do the following:
public interface IArea
{
double CalculateArea(double length, double width);
}
Now when you implicitly or explicitly implement your interface your declared as public and taking the parameters.
Now when you build your object:
public static class Rectangle : IArea
{
public double CalculateArea(double length, double width)
{
// Logic to calculate.
}
}
That would be an implementation.
I modified the answer a bit, so in the Main
you would do:
Rectangle rectangle = new Rectangle();
Console.WriteLine(rectangle.CalculateArea(10, 5));
That would build your object, then should return
the area to be written to your console.
Upvotes: 0
Reputation: 429
To resolve your error, you should write : public interface IArea {...}
.
Indeed, the public
keyword is used for the classes, but not for the interface, which is internal by default. Or, the processingArea
method is public, where as IArea is so internal. If you change thus your code, it will works !
Upvotes: 0
Reputation: 71563
By not specifying a visibility modifier for the interface IArea, C# uses the default of "internal", so it's only visible to other types in the same assembly. However, your shapes are public, so as written, it's possible to refer to the concrete class from a location in code that isn't allowed to know about the interface it inherits from. This isn't kosher.
To fix it, simply change IArea to be a public interface by adding the public
keyword to the definition.
Upvotes: 1
Reputation: 61349
That error means you have a public
method, that takes a parameter (in this case, an IArea
) with a more restrictive access modifier. internal
is the default, so your interface probably needs to be:
public interface IArea
{
...
}
When you think about it, the error makes sense. If you call a public method from another assembly, but can't see the parameter type (because its internal
), you actually can't call the method! The compiler detects this for you, and throws the error.
Upvotes: 1
Reputation: 3813
Your classes that implement IArea
are public. Therefore IArea
should be public.
public interface IArea
{
double CalculateArea();
}
Upvotes: 1