.Net Variable Access Modifiers

Access modifiers are something that are confusing at times, and the syntax for them in C# irritates me a little (I wish it worked more like C++).  This is a quick Access Modifier summary

There are five types of access modifiers for variables and methods in .Net
1. Public 
This is the default for any member of a Struct that isn’t specifically labelled otherwise. A public member is accessable by any caller – hence the name public.

2. Protected
This is more restrictive than public, in fact you can only access members marked Protected if you are inside the class itself or a class that is inheriting the class.  Lecturers I had at uni used to espouse the view that you should always use protected instead of private in most situations, as it allows deriving classes to access your classes internals. 

With .Net’s excellent support for accessors, i’m not sure if its as necessary to mark internal use storage variables as protected instead of private anymore.  In the end, it’s a personal choice.

3. Private
Private variables are completely locked off. The only place they can be accesses are within the class they belong to.  This allows accessors and methods to use them (although best practices may suggest even your own methods use accessors instead of directly accessing private variables), but no-one outside the class, not even inheriting classes.

So far, these are the same as is implemented in C++ and Java, however .Net, with its emphasis on assemblies, added two new access modifiers.

4. Internal (friend in visual basic.)
This is similar to private, in that most callers are denied access, however any caller within the same assembly can access members marked this way. This has some interesting side effects, as deriving classes within the assembly treat these members the same as protected (they can access them fine), however derived classes in other assemblies treat this as private, and can’t access it.

5. Protected Internal
Basically combines internal and protected, so that derived classes can access members marked so anywhere, and also callers within the assembly can access without problems.

 Personally, i’ve never found a situation so far where i’ve needed the last two.  Internal is interesting as it allows you to basically set protected for the members, so your derived classes can use them, but denies this ability to anyone else who derives from your class in another assembly.  The down side is, it gives more access to your own classes within the assembly than protected would, so you give up a layer of compartmentability as well.

Theres the quick overview. I’d be very interested to hear of any situations you have come across that required more than private or public, particularly any situations where the two new modifiers were helpful.

 –EDIT–

There was a question in the comments about what “const” and “ReadOnly” were, and how they differed. My answer is a bit long and so i’ve added it to this post in full.

This was a bit confusing for me too at first, coming from C++ and Java where we have const, working out what exactly ReadOnly is and, in particular, how it differs from const it difficult.

Const
Const is reasonably easy to explain, it is short for constant. A constant variable is one that is unchanging throughout the life of the application and is used traditionally for literal unchanging values that are used in multiple places throughout your software.

A common example of this is Pi.  If you use the value for Pi in several places you can put 3.1415 in different places all through the code, and run the risk of mis-typing it at some point.  Or, you can use const float PI = 3.1415 and just use PI throughout the application. Also, if you decide later you want to use a more accurate version, or less accurate version of pi for your calculations, you don’t have to do a search and replace, you can just change it at one place in the code. 

Basically, it was a way to mode code easier to read and maintain by labelling literal values.

ReadOnly
ReadOnly looks like it might be Visual Basic only, and is basically a less restrictive version of const.  It can be used on variables or properties.
When used on a property, its the same as only setting a get property on a c# property. You can retrieve the value but not set it from outside the class, but if you’re inside you can do what you want to the underlying data type.

When applied to variables, it acts like const. You can set a value in the definition and not change it at any other point during the application.  The only exception is a ReadOnly variable that is defined in a class is treated like an ordinary variable whilst inside that class’s constructor. The idea then is you can set a variable to readonly, do some calculations or file reading in the constructor, set the value of the variable, and then, as you leave the constructor, it sets like quick-dry cement and becomes a const variable for the rest of the life of the class.

I hope that helps clear things up, feel free to comment if something is still unclear.

An argument for the Earth being the center of the universe.

http://blogs4brownback.wordpress.com/2007/05/18/heliocentrism-is-an-atheist-doctrine/

 It’s…. interesting.. I suppose.  Scary in some ways in that I can’t work out if it is meant to be satire, or if he’s deadly serious.  Perhaps worse are the comments on this post – even if he is serious, attacking him personally will just validate him.  I see this all the time against Christian fundamentalists, the supposedly smart people calling them stupid (or worse).

Don’t you realise that suffering and martyrdom are central precepts of the Christian faith? Nothing feels better or more vindicating to a true fundamentalist than being hated.  In the days leading up to his death, Jesus was hated.  When you attack these people, you make them feel like Jesus.  This inspires them to continue.

Everyone has a right to their opinion, and the right to publish it.  My only problem with fundamentalists is their idea that everyone else shouldn’t have a right to their opinion, and should be forced to follow theirs.  Their kind walked America with a bible and a gun, and if the natives refused the first they got the second.

My ancestors were no better i’m sure, those that weren’t locked up at the time, when they settled Australia.  I thought we’d gotten past that though.

 As for the Earth being the center of the universe – doesn’t really bother me either way.  I know of plenty of scientific research that has supported otherwise, but if it were all proved tomorrow to be wrong, it wouldn’t shatter my world view much.

I still wont believe in your God.  I don’t like him very much.

A namespace does not directly contain members such as fields or methods

A namespace does not directly contain members such as fields or methods – another interesting and completely unhelpful error message courtesy of .Net.  Thankyou for flying 😉

This particular error caused me a great deal of problem in a piece of software I had designed and written that used Web Services Enhancements (WSE) 2.0 SP3, using Visual Studio .Net 2003.  There wasn’t a lot of information around but I eventually solved the problem – actually, it was quite simple.

 As anyone using web services enhancements knows, you right click the project name, choose “Web Service Enhancements” and check the little check box to enable WSE for your project.  This adds an app.config to your program if you dont already have one with a little configuration information designed to make WSE work for you.

 Turns out if your app.config gets invalid somehow, that can cause this error to occur.  I had two things in my app config, both managed by the designer – the wse settings and a dynamic web service link.  The fix was simple, I set the web service link back to static (to remove its dependancy on the app.config) and disabled wse 2.0.  Then I hit compile.  It wont compile of course, without the WSE addons it needs, but I figure it’d ensure that the changes I made had propogated.  I then deleted the app.config file altogether and re-enabled wse 2.0.

This creates a new app.config with the appropriate settings, and whatever was corrupted about my old one was now corrected.  All I had to do then was reset my web service link to dynamic, update the reference and hit recompile and it worked like a charm.

So just remember a possible reason for this error can be a corrupted app.config.  Regenerate it (or re-write it if you manually wrote the file) and it can vanish nice and easy.

Clashtastic

Heres a cool post with a bunch of cover versions of classic Clash songs.  If you don’t know who the Clash are, shame on you. They only released the greatest album ever recorded.  (London Calling).

 I really can’t endorse this guy’s comparison of the Clash and Pearl Jam however.  I can’t stand them, and really can’t see that many points of similarity between the two bands.  Oh well. 

http://berkeleyplace.wordpress.com/2007/05/10/clash-covers-a-to-z-or-30-clashtastic-songs/

Microsoft Security Patch Recall – A technical look

I found this post particularly interesting, not just because it features a microsoft programmer sticking his neck out and saying “yeah, that was my fault” for the failure of a patch, but because it delves into the patch itself, what it did, how it did it and why it failed.

It’s quite a good read.

http://blogs.msdn.com/oldnewthing/archive/2007/05/04/2402028.aspx