tags:

views:

34

answers:

1

When I have the following bit of code in my C# Class Library, it totally breaks the library for everything else.

namespace DesktopOCA.Infastructure
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
    }
}

For example this is also in that project:

namespace DesktopOCA.Infastructure
{
    public static class RegionNames
    {
        public const string MainRegion = "MainRegion";
    }
}

When I dont include the

private static ISessionFactory _sessionFactory;

line everything else in my solution can see RegionNames.MainRegion. But as soon as I make any reference to an ISessionFactory, it breaks. That particular class library still compiles fine. I can add it as a reference in other parts of the project, but it's like there is nothing there.

I get the error

Error   40  The name 'RegionNames' does not exist in the current context    

Any help would really be appreciated here, this seems really weird to me.

+1  A: 

Static classes can only contain static members. Either move the static keyword from the RegionNames class to the MainRegion member or get rid of static altogether. It depends or what you're doing with that class.

See the MSDN doc for more info: http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

edit: also, if you want to keep MainRegion as static, you'll have to change const to readonly.

edit2: I was using Reflector today to look at System.Data.Common.ADP and noticed const without the static keyword within the static class. The last section "Static Members" of the above link says:

Although a field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type. Therefore, const fields can be accessed by using the same ClassName.MemberName notation that is used for static fields. No object instance is required.

It doesn't, however, say whether this works within a static class and is fairly ambiguous in its wording: there cannot be static members, const cannot be static, but that const can be accessed as if it is static.

Jim Schubert