-3

lets say, i have enum:

MyNameSpace.MyLongEnumName { Green, Red }

How can I create a shorthand of that enum in base (i.e. a) class, so I can use it easily in derived (like b) class?

i.e. first.cs :

namespace Foo
{
    public class a
    {
        // phseudo-code,like:  
        using xx = MyNameSpace.MyLongEnumName;
    }  
}

second.cs :

namespace Foo
{
    public class b : a
    {
        // phseudo-code,like: 
        xx myProperty = xx.Green;
    }  
}

p.s. main point here is to avoid using ... in the top of .cs files of derived classes.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 2
    Well, you cannot have `using` directives like that within class declarations. `using` directives have to come before all other elements such as classes or other enums in (or outside) a namespace. So, you could move your `using` directive above the `class a` declaration while still keeping it within the `Foo` namespace. But yeah, that is not going to help with regard to the source files of derived classes. Well, though luck, i guess... :-( –  May 25 '19 at 21:30
  • What's wrong with `var myProperty = MyNameSpace.MyLongEnumName.Green`? I mean you could short it to `var myProperty = MyLongEnumName.Green` by `using MyNameSpace;` – Christian Gollhardt May 25 '19 at 22:14

1 Answers1

1

I am not realy sure what you want to achieve.

For example

namespace MyCompany.MyProduct.MyServices.MyColorService.Enums
{
    public enum Color
    {
        Green,
        Red
    }
}

Now we want to use it:

using MyCompany.MyProduct.MyServices.MyColorService.Enums;

public class Foo
{
    public void Bar()
    {
        var currentColor = Color.Green;
    }
}

Compare this to:

public class Foo
{
    public void Bar()
    {
        var currentXx = xx.Green;
    }
}
  • On the first example, I Immediately see it is a color. The green one. Also I immediately see, it is our custom color, and not the one from System.Drawing.
  • On the second example, I only know, it is some green of xx? Is it a green soldier? Is it environment friendly?

What ever you are trying to do, it makes things worse.

Main point here is to avoid using xx = MyNameSpace.MyLongEnumName; in the top of .cs files of derived classes.

That's not how namespaces working. Namespaces are there, to make sure, you have no naming conflict with other libraries. They are here, to ensure that you are operating on the type you realy want. That's why your derived class also has a own namespace.

Or in other words: What you want to achieve is not possible. Even when it would be possible, it probably make things worse!

Tldr: Make sure, you organize your Code. Create a balance between too verbose names and too simple names.


Aside from that, write Color, set the cursor over it and press ctrl + ., then enter. You will notice Visual Studio will automaticaly imports for you the namespace, or offers a list, if the name is ambigious. Also we have tools like Resharper, which are importing the namespace while using intellisense ;)

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111