Click here to Skip to main content
15,610,426 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello coders, I require your help to continue with my project.
I'm an amateur newbie btw, this application is for my personal joy.

So I have this code:
namespace GC
    public partial class MyControl : UserControl
        //-- Design stuff
        //-- Properties
        //-- Events

I want to make a List<MyControl> that is outside the MyControl class, accessible by all classes/structs/whatever within the GC namespace, but not accessible by anything outside that namespace.

How can this be done?
Sergey Alexandrovich Kryukov 30-May-12 11:43am    
You apparently do not understand namespaces. There is no such thing as "accessible by anything outside namespace".
Other than that, what's the problem?
Mossmyr 30-May-12 12:00pm    
well... "a List accessible by all classes/structs/whatever within the GC namespace, but not accessible by any class/struct/whatever in another namespace" might have fit better.

I understand that what I'm writing is confusing, I dont know all the right words yet. I've been coding for like a month tops.
I'm trying to make a List which I can add MyControls to, but all access modifiers I've tried so far still make the List accessible by other namespaces (instead of only GC).

You are looking for the correct Access Modifiers[^]. Looking at the explanations planted on Accessibility Levels (C# Reference)[^] I suspect you can only achieve this if your namespace is restricted to one assembly. Then the access modifier internal would work for you.


Share this answer
fjdiewornncalwe 30-May-12 10:13am    
+5. Took me a bit to understand what the OP wanted. I think you nailed it.
Manfred Rudolf Bihy 30-May-12 10:22am    
Thanks Marcus! :)

There is even a quote on the first MSDN site I linked to that looks suspiciously wrong: "Classes and structs can be restricted so that only the program or namespace they are declared in may use them". If one reads the description for the Accesibility Levels no such statement can be found. Strange!
Sergey Alexandrovich Kryukov 30-May-12 11:53am    
This is totally wrong; and this is similar to the confusion OP has. Again, I tried to explain it in my answer, please see it.
Mossmyr 30-May-12 10:49am    
Yes, it is an access modifier I'm looking for, but I can't find the right one.
The List I'm trying to make can't be defined outside a class (at least I think so?), so I need a class, but visual studio throws an error when I try to make the class "protected", "private" or "protected internal".
Making the class internal still makes it accessible from another namespace...

I just thought of another problem with what I'm trying to do, I think I'll need to redo the entire code.
Sergey Alexandrovich Kryukov 30-May-12 11:52am    
Very useful, my 5, but it's not quite all -- it needs some conclusions just because OP is really lost and failed to draw correct conclusions from his knowledge.
So, please see my answer.
Please see my comment to the question.

A namespace is not a unit or any other kind of package controlling access. Such unit or package is the assembly, but there are no any correlation between assemblies or namespaces, that is, one assembly can define types in several namespaces, and the same exact namespace can be used in different assemblies for defining of types. The same stands for using definitions in different namespaces: there are no any barriers. More exactly, the namespaces only define how things are named, not access.

When it comes to access, the assembly boundary make access modifiers public different from internal or protected internal, the key word "internal" means "public but withing the same assembly".

Finally, even the access controlled by access modifiers, across assemblies or not, is fully transparent with the use of Reflection. With Reflection, all private, protected, internal and internal protected declarations are equally accessible, as well as public.

Share this answer
Mossmyr 30-May-12 12:03pm    
I think I got it.
But what is Reflection? Nevermind.
I can do like Joshi suggested then.
Sergey Alexandrovich Kryukov 30-May-12 12:30pm    
Great. Good luck,
Joshi, Rushikesh 30-May-12 15:57pm    
Thanks Mossmyr, my solution helped you what else I want :)
Sergey Alexandrovich Kryukov 31-May-12 17:16pm    
[Probably misplaced -- SA]
Espen Harlinn 31-May-12 15:25pm    
Good points :-D
BTW: Seems you ware hit by the elusive uni-voter ...
If this is in a library then, create a DLL and include this DLL in References of your outside projects.

Now by using you can include this namespace to your outside project and you can start use it.

using GC;

Even if you are referring in project you have to include by using statement.

Refer Understanding Namespaces in C# 2.0[^] for more reference.

Share this answer
Mossmyr 30-May-12 10:53am    
Thanks man, but I already know how to use GC's classes/methods from another namespace. What I dont know is how to prohibit access to certain classes in the GC namespace.
Sergey Alexandrovich Kryukov 30-May-12 11:51am    
You apparently don't know that. I explain it in my answer, please see.
Joshi, Rushikesh 30-May-12 11:55am    
MossMyr, how do I know from your question that you know basic of Namespace? and that is not a valid excuse for Down Voting me :). But thank you
Mossmyr 30-May-12 12:07pm    
I have not down voted you :o
I wonder who did.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900