3

Consider the following case

public class SomethingWithAReallyReallyAnnoyinglyLongName{
    public struct Names
    {
        public const string SomeConstant = "Hello";
        public const string SomeOtherConstant = "World";
    }
}

Is there a way of referencing SomethingWithAReallyReallyAnnoyinglyLongName.Names.SomeConstant without having to reference SomethingWithAReallyReallyAnnoyinglyLongName, when outside of the SomethingWithAReallyReallyAnnoyinglyLongName context?

// Won't work "Struct Name is not valid at this point."
var names = SomethingWithAReallyReallyAnnoyinglyLongName.Names;
SomeFunction(names.SomeConstant, names.SomeOtherConstant);

// Won't work "Cannot access static constant..."
var names = new SomethingWithAReallyReallyAnnoyinglyLongName.Names();
SomeFunction(names.SomeConstant, names.SomeOtherConstant);

The long class name is auto-generated, so I can't change that, but I could probably change anything about the Names struct (make it a class, change the consts to not be const, etc.).

Any ideas?

Daryl
  • 18,592
  • 9
  • 78
  • 145
  • No - `const` fields are effectively `static` and must be qualified by the class name. Is the entire class generated or just the name? – D Stanley Apr 09 '15 at 16:00
  • @DStanley the entire class is generated. But I can tweak the CodeDom (which is what I'm doing to add Names) – Daryl Apr 09 '15 at 16:01
  • 2
    You can use alias like using Shortcut = SomethingWithAReallyReallyAnnoyinglyLongName.Names; and then just Shortcut.SomeConstant – Ondrej Svejdar Apr 09 '15 at 16:02
  • Last I read, C# 6 was going to have something like that, but I don't know if that's finalized yet, or if it works with non-static classes. – Joe Enos Apr 09 '15 at 16:07
  • Maybee [this](http://stackoverflow.com/questions/481725/with-block-equivalent-in-c) can help. – Eminem Apr 09 '15 at 16:12

2 Answers2

6

Well, in the files consuming the class you can do this:

using SwarralnNames = SomethingWithAReallyReallyAnnoyingLongName.Names;

Then you can type SwarralnNames.SomeConstant

Not ideal, since you need to apply this using in each file that wants the appropriate 'shortcut' name, but it can really help clean up multiple references in the same file if you can't control the original name.

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
0

Can you move the Names type outside of SomethingWithAReallyReallyAnnoyinglyLongName? I really hate nested types for that reason. If not, then how about this solution:

public struct NamesConstants
{ 
    public const string SomeConstant = "Hello";
    public const string SomeOtherConstant = "World";
}

public class SomethingWithAReallyReallyAnnoyinglyLongName{
    public struct Names
    {
        public const string SomeConstant = NamesConstants.SomeConstant;
        public const string SomeOtherConstant = NamesConstants.SomeOtherConstant;
    }
}

This way you could refer to the constants in NamesConstants without having to fully qualify the nested type, and the nested type simply uses the same constant values that are in NamesConstants.

Erik
  • 5,355
  • 25
  • 39
  • The NamesConstants is specific to the SomethingWithAReallyReallyAnnoyinglyLongName. There are other similar class that will have similar NamesConstants, so this solution won't really work. – Daryl Apr 09 '15 at 16:37
  • Well, it'll certainly work, you would just have a different `NamesConstants` structure for each one of these classes (with a different name for each obviously). This might scale better than the `using` alias strategy, but it depends on how many places you're going to be accessing these values and how many of these classes you have. – Erik Apr 09 '15 at 16:48
  • I have some 100+ classes with long names, each one will contain a struct named Names. NamesConstants will have to be unique per 100 classes, so it really won't work well. – Daryl Apr 09 '15 at 17:09