I am trying to create a setup for a class I am making so when it is created a manager class can be setup for it, and it might require to call functions in that class via an interface
These calls are not always required, and the manager may not always be the class that called this class, so a simple return value and use it form the manager class does not meet the requirements.
What I am trying to do is following code. (Tried to strip as much unnecessary out as possible)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
interface Itester
{
void LoadTest();
}
public partial class Form1 : Form, Itester
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
World testWorld = new World();
testWorld.SetManager(this);
testWorld.InitializeWorld();
}
void Itester.LoadTest()
{
//random action I want run
label1.Text = "Ran LoadTest()";
}
}
public class World
{
Itester worldManager;
public World()
{
InitializeWorld();
}
private void InitializeWorld()
{
worldManager.LoadTest();
}
public void SetManager(Itester test)
{
worldManager = test;
}
}
}
I get this error based on it. The error refer to the “public void in the World class”
Error 1 Inconsistent accessibility: parameter type ‘WindowsFormsApplication1.Itester’ is less accessible than method ‘WindowsFormsApplication1.World.SetManager(WindowsFormsApplication1.Itester)’ XYZ Location Form1.cs 316 21 Feudal World
What I would have expected to happen was that.
Form1 class creates a local instance of the World Class, then runs its constructor (does nothing), then it sets itself as its Manager (could in theory be another class implementing the Itester interface), finally it calls the World Class again and ask it to initialize the world, where I would have expected the world class to call the Form1 instance and have it update the label on the button.
lvl1:Form1 -> lvl2:World -> lvl3:Form1(or other manager) -> return void to lvl2:World -> return void to lvl1:Form1
What am I missing, why does this not work?
Your interface does not have accessibility modifier, so it is assumed
internal. Change it to public:The reason you need to do this is because the method
SetManageris public, thus anyone that can consume the SetManager method must also be able to see the interface.Consider that
SetManagercan be called from any assembly because it is public. So if someone were creating an assembly, they could reference yours and callworldInstance.SetManager. Let’s call that assembly BigHappyAssembly.Now consider the first parameter of
SetManagerisITester, however it is internal. When BigHappyAssembly tries to call SetManager, it would be presented with a problem: What am I suppose to give as the first parameter? It doesn’t have access to theITestertype, so it doesn’t know that is what the first parameter is.To prevent this happening in the first place, the compiler stops you from introducing this problem. It’s warning you that you have created a public member, that anyone can call, however not everyone will be able to know what the parameters are.