views:

72

answers:

4

Very often when Visual Studio generates code (for example generating an event handler stub) it omits access modifiers for members. I would like it to stop doing this. Is there a setting for this?

+1  A: 

just get used to the absence of a modifier meaning 'private'. you'll have to anyway when you look at other peoples code.

mcintyre321
I don't like quitting :(
Andrei Rinea
+2  A: 

There might be snippets somewhere in Visual Studio that are generating this code for you. You'll have to locate the snippet for the specific action you want to change. For example, I wanted to automatically make classes public when I declared them so I edited the class.snippet file.

I changed

<Code Language="csharp"><![CDATA[class $name$
{
 $selected$$end$
}]]>
  </Code>

To look like

<Code Language="csharp"><![CDATA[public class $name$
{
 $selected$$end$
}]]>
  </Code>
msergeant
+2  A: 

The code generated by VS is probably generated using CodeDom. I don't think you can influence the way it generates the code...

I fear that there is no way to do that. Or you can do that post generating using a clever Regex?

Side note: I use to hate when no modifier is specified, but you get used to it. Sad but true... Anyway, when there is no modifier, it is as private as it can be, one exception being properties.

Philippe
+1  A: 

Visual Studio is not tailored to be a code generation tool. If you want to do code generation (and you'll get many benefits if done right), you'll have to look into other tools that can integrate or work along with Visual Studio.

I am developing a generative, model-driven methodology called ABSE (Atom-Based Software Engineering) and a reference implementation of it through an IDE called AtomWeaver. It's work in progress but you may want to keep an eye on it: http://www.abse.info

Visual Studio ships with T4 templates. I never used it but from what I've heard from others, it's not worth the hassle. For now, I guess a safe bet would be CodeSmith.

Rui Curado
I don't think this is a correct answer to the question - unless the question is confusingly worded.
mcintyre321
As the OP is clueless about an erratic VStudio behavior (and me too), I suggested using a more mature/stable code generator like CodeSmith.
Rui Curado
The code gen in question is when you haven't written an event handler yet, you get prompted if you'd like to create one. The one it makes doesn't have a private modifier. I don't think in that case you'd want to switch to use a tool like codesmith to do it. TBH its quicker just to write the 'private' in yourself.The correct answer is probably 'no, there is not a setting'
mcintyre321