1

I thought that assembly is a project in a solution in Visual Studio, but then I was trying to understand inheritance and internal/protected access modifiers and I got lost.
*Project1 and Project2 are separate VS projects in one solution:

namespace Project1
{
    public class C1
    {
        internal int field1;
        internal protected int field2;
    }
}


namespace Project2
{
    public class C2
    {
        public static void f(C1 c1)
        {
            c1.field1 = 1;  //no problems here
            c1.field2 = 2;  //neither here
        }
    }
}


In Project2 I have no inheritance so both fields field1 and field2 should be inaccessible but there's no errors at all. (I added Project1 into Project2 References and add using Project1 in Project2)

user3616181
  • 975
  • 2
  • 8
  • 27
  • Are you absolutely sure about this? I get the expected behaviour. – Rotem Nov 11 '14 at 14:31
  • 2
    I guess you're still in same project despite of the different namespace. Can you confirm this? May be `C1` class exist in both projects by mistake? If not, can you share some screenshot or a sample project? Also make sure you're not using `InternalsVisibleToAttribute` – Sriram Sakthivel Nov 11 '14 at 14:33
  • @Rotem I'm afraid yes https://i.imgur.com/P45J6Sc.png https://i.imgur.com/n43A3EP.png – user3616181 Nov 11 '14 at 14:39
  • Here's a dumb question. Could you try running the code? Does it just let you compile, or does it let you run? – Shriike Nov 11 '14 at 14:41
  • @Shriike I tried to run it and I didn't get any compilation or run errors – user3616181 Nov 11 '14 at 14:42
  • Did you first create `C1` and `C2` in the same project? Try cleaning the solution, removing all assemblies from the bin folders and compile again. – CodeCaster Nov 11 '14 at 14:42
  • @SriramSakthivel I don't even know what InternalsVisibleToAttribute is, so I did not use it intentionally :) – user3616181 Nov 11 '14 at 14:43
  • @CodeCaster No, all classes was created as you can see on screenshots (separately) – user3616181 Nov 11 '14 at 14:44
  • Delete all the files in Bin, then try to recompile and see if that compiles fine(Go and check binaries are created in bin). If it does, can you share the project with us(through some file sharing website)? so that we can also see the problem? – Sriram Sakthivel Nov 11 '14 at 14:47
  • seems that cleaning filed in Bins doesn't help. I tried to make a new solutions with this projects and share it with you. Just wait a sec :) – user3616181 Nov 11 '14 at 14:51
  • Open Configuration Manager and check Build for both libraries. I guess your first try was without internal modifier, then you added it and works in unexpected way. That's because your libraries are not rebuilded. – Renatas M. Nov 11 '14 at 14:51
  • http://www.speedyshare.com/FnkM2/Solution1.zip All I did is copy paste the code you can see above, add Project1 to Project2 references and add `using Project1` co Project2 code – user3616181 Nov 11 '14 at 14:58
  • 1
    Your project is broken, I managed to add the files in place and tried to compile. Compiler error occurs without any surprise. I'm voting to close as *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior* If you can share a code which can reproduce the problem. I'll retract my vote. http://imgur.com/aWkpu2B – Sriram Sakthivel Nov 11 '14 at 15:04
  • Eemm... you hava all code above, in question. I didn't make anything more than I wrote in comment before :/ It is new Solution with new Projects. Just copy paste the code. I don't know what could get wrong. – user3616181 Nov 11 '14 at 15:06
  • That's weird. Seems the project is really broken... – user3616181 Nov 11 '14 at 15:11
  • As you can see the image I linked in above comment, I can't reproduce the problem. Also your project is broken. I mean two program files are missing, C1 and C2 classes was not added in project. Try to open the solution which you shared us in Visual studio and you'll see what I'm talking about. – Sriram Sakthivel Nov 11 '14 at 15:13
  • Yes I noticed that. Now I'm trying to share correct solution – user3616181 Nov 11 '14 at 15:15
  • OK I tried open this and it worked (except no expected errors) http://www.speedyshare.com/Qfhep/Solution2.zip – user3616181 Nov 11 '14 at 15:24
  • "Your field1 and field2 are public..." Eh, some error in copying – user3616181 Nov 11 '14 at 15:29
  • http://www.speedyshare.com/WbSPP/Solution2.zip access modifiers are improved, main added, bin folders deleted befor building. Still the same – user3616181 Nov 11 '14 at 15:34

2 Answers2

1

Your solution works, because it contains errors that Visual Studio can ignore.

You have a fully functional project Project1, containing a Main(). This is the project that is set as startup project, which can run because it contains no errors.

Not all project in your solution have to successfully build in order for one project to run; just the projects that the project you want to run depends on. In this case none.

Right-click Project2, click "Set as StartUp Project" and try to build or run it: you'll see the compiler errors you're expecting.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • https://i.imgur.com/XhKfGPE.png field1 error is a bit strange. And I'm still don't know why VS doesn't highlight this lines as errors (as it does at others screenshots) – user3616181 Nov 11 '14 at 15:43
  • Because `field1` is marked `assembly` and `field2` is marked `famorassem` in IL. The compiler simply cannot see `field1`, while `field2` _can_ be seen, as it is `protected`, and thus can be used by classes inheriting from `C1` (even from different assemblies despite it being `protected internal`, see [Access Modifiers](http://msdn.microsoft.com/en-us/library/ms173121.aspx): _"The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly."_). – CodeCaster Nov 11 '14 at 15:55
  • OK, I get it, but why all you guys get nice warnings during coding and I have so much problems just to see the problem really exists? – user3616181 Nov 11 '14 at 15:59
  • Because there were multiple errors with the different projects. Errors are cascading, if at the beginning something goes wrong, you won't see errors generated further on. For example when in your bin folder there was an assembly containing `C1` with public fields. – CodeCaster Nov 11 '14 at 16:00
  • 1
    OK then, lets assume that I understand what's go wrong and I hope I'll never get something similar in the future. Thanks for all your help guys – user3616181 Nov 11 '14 at 16:11
0

Internal is an access modifier in which the class, method, or property is exposed to any class that is also embedded within the same assembly.

However classes, methods, or properties that are declared internal are NOT exposed to classes belonging to other assemblies.

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118