Shorten NameSpace question

I have a large enum structure like this:

public enum MyEnum
{

Value1 = 100,
Value2 = 200,
..................

}

Then I have a switch statement for all the enum members:

switch (somevalue)
{

case MyEnum.Value1:

Dosomething;
break;

case ..........

}

Since I have a lot of these I would like to shorten the case statements, avoiding the 'MyEnum' type definition. Many programming languages (like JScript or Delphi) allow this using the 'With' statement. In C# I tried the 'using' keyword like this:

using (MyEnum)
{

switch (somevalue)
{

case Value1:

Dosomething;
break;

case ..........

}

}

That doesn't work. What is the correct syntax to use




Answer this question

Shorten NameSpace question

  • CrispinH

    You can rename the enumeration at the top of the code file via:



    using System;
    using System.Windows.Forms;
    [...]
    using ShortName = MyCompany.MyProduct.ThisIsAVeryLongTypeName;



  • Kiranboi

    The 'with' statement in Delphi can (as far as i know) only be used on variables (instance of a type), not on types. The same goes for using. I don't know if what you want is possible. But if i had only read your thread subject and not your actual question i would have said:

    Yes you can shorten the namespace name by doing the following:

    using Gen = System.Collections.Generic;

    Now you can call all the classes in the System.Collections.Generic namespace by using Gen.<Classname>.


  • GGanesh

    I actually knew that I could make aliases like that, but the namespace is not that long, and I do like clean code, I was just too lazy to write it

    So Intellisense is the perfect solution, clean code without all that typing.

    I didn't get what I asked for, but what I needed and that's better.



  • dr4nra

    As mentioned before using is not the thing you need, using have completely different meaning (check language syntax for more info).

    Generally you can't cut type name here and actually there is no need for this, even if you have hundreds of values you can simply copy/paste or use VS 2005 IntelliSense: type "switch" and press TAB, then type 'somevalue' and press ENTER, VS will create all possible case statements.

    Another way is to declare constants in the same class, but you will need to write much more code for this (each const must have value, etc.).



  • Maigo

    Thanks, that works, I guess I'll have to learn IntelliSence some more, it actually lives up to it's name.

  • Shorten NameSpace question