Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best access modifier? 1

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
In my solution I've just added a second class. I want my main class to be able to access some of the members of the second class, which won't be used in any other projects; its functionality is dependent upon the first class, though it's not really a subclass - more of a helper class.

What is the most appropriate access modifier for the methods/properties of the second class which the first class will need to use? I typically make my methods/properties either 'public' or 'private' depending upon whether trans-class access is necessary.

But I'd like to have a little better grasp of when I ought to use 'internal' or 'protected internal', and what the benefits are of doing so.

Thanks!
 
if your class methods are private, the only the class itself can access them.

If they are protected, then only the class and its subclasses can access them (even outside the assembly)

If they are internal, then only the class and its subclasses can access them (and only in the same assembly)

If they are protected internal (which means protected or internal) then it allows the member to be available anywhere in the assembly in addition to any classes inheriting the class (in the assembly or not).


Hope this helps
 
First, decide how you want to expose the encapsulating class first.
Code:
[COLOR=green]/* Exposed to other projects that reference this assembly. */[/color]
public class MainClass
{
    [COLOR=green]// Method is visible outside assembly[/color]
    public void Foo(){}

    [COLOR=green]// Method is visible within the assembly only.[/color]
    internal void InnerFoo(){} 

    [COLOR=green]// Method is visible in this class only.[/color]
    private void Goo(){}


    [COLOR=green]// Class for this class only.[/color]
    private class InnerClassA{}

    [COLOR=green]// Class for this assembly only.[/color]
    internal class InnerClassB{}

    [COLOR=green]// A public class where MainClass acts as a namespace[/color]
    public class InnerClassC{}
}

[COLOR=green]/* Available within the assembly only */[/color]
internal class HelperToMainClass
{
  [COLOR=green]// All public members are visible within the assembly only.[/color]
}
In your case, I would suggest having the helper class as either:

a) an internal class, so it's still available to other classes in the assembly.

b) a private class in the main class, so that the helper class is exclusively available only to main class, and not to other classes (see InnerClassA above).

If (b), the partial keyword might be handy in case the main class codefile gets too big.
On both options, the class members can be public or internal.

To know more about the different accessibility modifiers / access levels, read on MSDN.


HTH, cheers! [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top