Pergunta

I'm working in a C# program now where there is a class ("GlobalConstants") of just global constants for things like error messages and return values. Most of these are in big enumerated types, so there are lots of places in the code where I have to type or read lines that look like this . . .

case GlobalConstants.SubunitAssemblyFailureEnums.FOD_ERROR:

so I'd love to be able to abbreviate the qualifier with something short, so it's . . .

case gsf.FOD_ERROR:  

Is there a way to do this with namespace or type aliasing in C#? I tried

using gsf = GlobalConstants.SubunitAssemblyFailureEnums;

... but the compiler just said "GlobalConstants.SubunitAssemblyFailureEnums is a 'type' which is not valid in the given context"

Edit: The Answer . . . was that my placement was wrong. My file is structured like this . . .

using System;
using System.Collections;
using System.ComponentModel;

namespace  ****
{

    class ****
    {
        method*** {   }

        method*** {   }

       ... etc...


    } //end class
} // end namespace

The first place I tried it was above the namespace directive with the other "using"s. The compiler didn't like that. Then I tried it inside my class, and it didn't like that either. But I got it to work just fine between the namespace directive and the class definition

Foi útil?

Solução

The using statement should work provided you include the entire namespace that GlobalConstants is in - for example, to add an alias for DayOfWeek use:

using dow = System.DayOfWeek;

Did you put it at the top of the file? Maybe it thinks you're trying to use the using() pattern for a disposable object

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top