Structure of Class Files

J

Jeff Gaines

Accepting that I am a hobbyist rather than a professional programmer I
have always set out my class files like:

using System;
using System.Collections.Generic;
etc.
namespace JGClassGenerator
{
internal class JTestADONETData
{
class functions
}
}

All the samples and examples I have seen are set out in that way.

I am looking at CodeDom to generate class files and it is putting the
namespace right at the top like so:

namespace JGCodeDom
{
using System;
using System.IO;
using System.Data.OleDb;

public class JTest
{
class functions
}
}

Is this a new style or a function of CodeDom?
I can force my usual layout by using code snippets (CodeSnippetCompileUnit
) but it seems to me that defeats the point of using CodeDom in the first
place (I assume it does some error checking to produce reasonable code).
 
W

Willem van Rumpt

Accepting that I am a hobbyist rather than a professional programmer I
have always set out my class files like:

using System;
using System.Collections.Generic;
etc.
namespace JGClassGenerator
{
internal class JTestADONETData
{
class functions
}
}

All the samples and examples I have seen are set out in that way.

I am looking at CodeDom to generate class files and it is putting the
namespace right at the top like so:

namespace JGCodeDom
{
using System;
using System.IO;
using System.Data.OleDb;

public class JTest
{
class functions
}
}

Is this a new style or a function of CodeDom?
I can force my usual layout by using code snippets
(CodeSnippetCompileUnit ) but it seems to me that defeats the point of
using CodeDom in the first place (I assume it does some error checking
to produce reasonable code).

Not that I know. It's probably a consequence of the fact that the root
class from which CodeDom generates code is CodeNamespace.

The CodeNamespace class has an Imports property, so the natural way to
generate source from this, is to first emit the source for the
CodeNamespace, and then its children.

The only way around this (AFAIK, working on projects using it), is to
extract them yourself after generation and move them up.
 
A

Anders Eriksson

Not that I know. It's probably a consequence of the fact that the root
class from which CodeDom generates code is CodeNamespace.
If you use StyleCop it will complain if the first version is used. I.e.
that using statements are before the namespace.

http://stylecop.codeplex.com/

// Anders
 
J

Jeff Gaines

StyleCop notwithstanding, I have used C# extensively in a professional
capacity, with teams of other programmers, and have almost never run into
a human being putting "using" directives anywhere except the top of the
file. There's no need to worry that using the former syntax will somehow
expose you as a "hobbyist". :)

It's kind of you to say so but there are plenty of other clues :)

I have written a class generator for churning out the boilerplate code
needed to access databases. It uses TextWriter.WriteLine extensively and
somebody suggested using CodeDom. Having just started looking at CodeDom
I'm having to find slightly different ways of doing things, the location
of the namespace/using directives being one of them. The inability to
declare classes as static being another.

It's an interesting experience but I'm finding that I'm using snippets in
many cases and I'm not sure if, at the end of the day, that's much better
than using TextWriter.WriteLine.

Anyway, it's keeping me out of mischief :)
 
A

Arne Vajhøj

Accepting that I am a hobbyist rather than a professional programmer I
have always set out my class files like:

using System;
using System.Collections.Generic;
etc.
namespace JGClassGenerator
{
internal class JTestADONETData
{
class functions
}
}

All the samples and examples I have seen are set out in that way.

I am looking at CodeDom to generate class files and it is putting the
namespace right at the top like so:

namespace JGCodeDom
{
using System;
using System.IO;
using System.Data.OleDb;

public class JTest
{
class functions
}
}

Is this a new style or a function of CodeDom?
I can force my usual layout by using code snippets
(CodeSnippetCompileUnit ) but it seems to me that defeats the point of
using CodeDom in the first place (I assume it does some error checking
to produce reasonable code).

It is not a new feature of the language. It has always so that
using directives (for namespaces not aliases) can be both in
the top of the file or right after the { of a namespace. In the
first case it has scope of the file in the second case it has
scope of the namespace.

I will recommend using the first form.

Reason:
- if you have a single file with multiple namespaces where
those namespaces uses types with conflicting short names, then
you have a design issue and limiting the scope of using
just hides that design issue
- it is very important to be able to quickly read and
understand the code - it is simply easier to find the using
directives when they are at the top of the file instead of
finding them at the top of the namespace

The last dash does not apply to generated code, because you should
not be reading it in the first place.

Arne
 
W

Willem van Rumpt

If you use StyleCop it will complain if the first version is used. I.e.
that using statements are before the namespace.

http://stylecop.codeplex.com/

// Anders

I prefer my "usings" to be on top of everything else also, but that's
not the point.

CodeDom is meant to generate code. Nothing more, nothing less. It
doesn't, nor should it, take style in regard. It should only churn out
source. Adhering to the any, or flavour-of-the-month source formatting
style is not one of its responsibilities.

I was merely explaining the probable cause of the "why".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top