I am new in c# development. I just trying to study the interface feature. Based on the articles and notes I read about interfaces, I tried to write a sample code to implement interface based on what I understood from those notes and articles.
But when I debugging the project I got a build error
“Inconsistent accessibility: parameter type ‘StartMachine’ is less accessible than method ‘SwitchBoard.switchPress(StartMachine)'”.
What is the problem here ?. or Did I implemented the interface in correct way ? or Is my concept about interface is wrong ?..
Please help.
Thanks in advance.
I posted my code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StartMachine s1 = new Machine1();
SwitchBoard switch1 = new SwitchBoard();
switch1.switchPress(s1);
}
}
interface StartMachine
{
void startMachine();
}
public class Machine1 : StartMachine
{
public void startMachine()
{
HttpContext.Current.Response.Write("Machine 1 Started");
}
}
public class Machine2 : StartMachine
{
public void startMachine()
{
HttpContext.Current.Response.Write("Machine 2 Started");
}
}
public class SwitchBoard
{
public void switchPress(StartMachine switchNum)
{
switchNum.startMachine();
}
}
You need to put a
publicaccess modifier in front of your interface.The Machine1/2 classes arepublic, but your interface is defaulting tointernal. This is less accessible than public.Set interface to public access modifer:
Alternatively, you could change your classes to be:
For more information on access modifiers, take a look on MSDN.
Correction::
The
Switchboardclass andswitchPressmethod arepublic. However, theswitchPressmethod is attempting to access theStartMachine.startMachine(ugh, ambigious naming) method, which isinternal(by default). You need to either change theStartMachineinterface to be public, or changeSwitchboard/switchPresstointernal.