Ok, so this is really wierd. I have a private member, and I want to use it into Form2. I've made a public static method, so that I can get that member into Form2.
Here is my code:
private static AppController appController;
private BreadRepository breadRep;
private CakeRepository cakeRep;
private SandwichRepository sandwichRep;
public Form1()
{
InitializeComponent();
breadRep = new BreadRepository();
cakeRep = new CakeRepository();
sandwichRep = new SandwichRepository();
appController = new AppController(breadRep , sandwichRep, cakeRep);
}
public static AppController getController()
{
return appController;
}
I've tried to make the appController from Form1 public, but I get even more errors. Right now I get this:
Inconsistent accessibility: return type 'exemplu_map.controller.AppController' is less accessible than method 'exemplu_map.Form1.getController()'
Any ideas ?
Update:
Here is my AppController class:
class AppController
{
private BreadRepository breadRep;
private SandwichRepository sandwichRep;
private CakeRepository cakeRep;
public AppController(BreadRepository breadRep, SandwichRepository sandwichRep, CakeRepository cakeRep)
{
this.breadRep = breadRep;
this.sandwichRep = sandwichRep;
this.cakeRep = cakeRep;
}
public void writeToFile(String file)
{
StreamWriter wr = new StreamWriter(file);
String writeMe = "";
foreach(Bread e in breadRep.getAll())
{
writeMe = writeMe + e.getAll() + "\n";
}
foreach (Sandwich e in sandwichRep.getAll())
{
writeMe = writeMe + e.getAll() + "\n";
}
foreach (Cake e in cakeRep.getAll())
{
writeMe = writeMe + e.getAll() + "\n";
}
wr.Write(writeMe);
wr.Close();
}
}
I've changed AppController to public, but I get again, more errors. The same error, but for breadRep, cakeRep, sandwichRep.
The problem is, as @Selman22 explained, that your method is public
, while its return value is internal
. (Classes are internal
by default.)
If both are public
or internal
, everything should work.
Making class public
appeared to be difficult due to dependencies on other classes. Moreover, it could be not the best, since by default it is better to keep things less accessible.
Making the method internal
solves the same problem from another end.
Anyway, @Selman22 was first :). I just added my two cents, so you should perhaps accept his answer :).
Accessibility is determined by the access level given to a type or member. It's important to note that the default access level is different for types/type members
The default access level for types is internal
The default access level for members is private
It is also important to note that private is not applicable to types (how can you construct a type if it is private - it could only ever construct itself), unless the type is nested in another type
Knowing this, it's easy to see why you would get errors in making your types public. In making a type public you are opening up your assembly to be referenced by other assemblies, meaning they can see the types within.
If your types are declared public and they have a public constructor, then it is expected that their public constructor can be called by an external assembly. It is because of this that all types which make up the constructor or any other public members of a type in your assembly must have public accessibility.
public class SomeClass
{
// This class and this constructor are externally visible
// The parameter of type SomeOtherClass must also be public in order
// for external assemblies to be able to construct this type
public SomeClass(SomeOtherClass someOtherClass) { }
}
// This would cause the issue you are having since this type is private but
// is included within a public contract (above), therefore the accessibility is 'inconsistent'
private class SomeOtherClass { }
I digress though - your issue, is with member accessibility
Your static member AppController
is marked private meaning it can only be seen by the Form1
class (I'm assuming that's the class it resides in)
The solution (as shown by Alex D) could be to make the member internal instead of private. This means that the member can be seen by any types in the same assembly. private is only visible to the type that declares the member
If you make it too accessible (public) you will get the errors as shown above. internal keeps the inner workings within your assembly, meaning you don't get these accessibility issues.