views:

72

answers:

2

I always see people giving me code (I mean code examples for questions on SO and the like) like:

    class MyData{
       ObservableCollection<Color> Colors;
       ObservableCollection<Fruit> Fruits;
       ObservableCollection<Pairs> Pairs;

       public void MatchCurrentSelection(){
            .....
etc
            }
       }
    } 

Everytime I start writing code in visual studio, it forces me to declare the visibility, and always included in a namespace. Do people jsut leave out the namespace as its irrelevant? And why am I always forced to set the visibilty? (It appears to automatically append private by default)

    namespace TheProject
    {
        public class MyData
        {
            private ObservableCollection<Colors> Colors;
            private ObservableCollection<Fruits> Fruits;
...

    etc

I have to be missing something here...What's the story?

Thanks

A: 

You should normally get the namespace that is the default of your project when you add a class to your project in VS 2008 SP1. This is what I got 2 minutes ago.

As far as the class is concerned, the default template has no visibility attribute, so it is internal (not private).

I guess the idea is that making it public has consequences, so you should do it explicitly.

Personally, I prefer my classes to be public by default, so I edited the VS template at: c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Inside, the class.cs file is the one to edit. VS has to be restarted for the change to take effect.

Timores
Making classes public by default is all right if you're building very simple standalone programs. I suppose. I don't know, I'm having a pretty visceral emotional response to that idea. I should go lie down.
Robert Rossney
I see classes as building blocks and the private stuff is what is inside the class (not the class itself).Sorry about creating such a bad feeling :-)
Timores
+5  A: 

If by "giving me code", you mean providing samples/examples on stackoverflow, then visibility and namespacing has probably been ommited for brevity, I know I do!

It's the same reason a large amount of sample code (here, and on places like msdn.microsoft.com) ommit most of the error-checking/handling, because having all that makes it harder to see the intent of the specific bit of code being provided as an example.

Rob