Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

Question on access modifiers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Flack - 20 Jun 2007 22:54 GMT
Hello,

Lets say I have this class definition:

 public class Class1
 {
   private int _privInt = 2;
   protected int _protInt = 4;

   public void SomeMethod(Class1 class1_)
   {
     System.Diagnostics.Debug.WriteLine(class1_._privInt);
     System.Diagnostics.Debug.WriteLine(class1_._protInt);
   }
 }

I have defined a private int field and a protected int field.  Since those
fields have restricted access, how come I can access them in SomeMethod
without any issues?  I would think that since _privInt is private to the
Class1 class, specifically the method parameter class1_, I wouldn't be able
to access it.

Can any private field of a class be readily accessed from within the
definition of that class, regardless of which instance of the class you are
using?

Thanks.
Jon Skeet [C# MVP] - 20 Jun 2007 23:19 GMT
> Lets say I have this class definition:
>
[quoted text clipped - 19 lines]
> definition of that class, regardless of which instance of the class you are
> using?

Yes - indeed, that's pretty much the definition of it. Various things
would be harder without that model.

What you might find peculiar is that the actual definition of the
accessibility is based on "program text", not on the type itself.

That means that private members of an enclosing type are accessible to
nested types too:

using System;

class Test
{
   private static int x;
   
   static void Main()
   {
       x = 10;
       new Nested();
   }
   
   class Nested
   {
       public Nested()
       {
           Console.WriteLine(Test.x);
       }
   }
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.