views:

3935

answers:

6

i want to create an alias for a class name. The following syntax would be perfect:

public class LongClassNameOrOneThatContainsVersionsOrDomainSpecificName
{
   ...
}

public class MyName = LongClassNameOrOneThatContainsVersionOrDomainSpecificName;

but it won't compile.


Example

Note This example is provided for convience only. Don't try to solve this particular problem by suggesting changing the design of the entire system. The presence, or lack, of this example doesn't change the original question.

Some existing code depends on the presence of a static class:

public static class ColorScheme
{
   ...
}

This color scheme is the Outlook 2003 color scheme. i want to introduce an Outlook 2007 color scheme, while retaining the Outlook 2003 color scheme:

public static class Outlook2003ColorScheme
{
   ...
}

public static class Outlook2007ColorScheme
{
   ...
}

But i'm still faced with the fact that the code depends on the presence of a static class called ColorScheme. My first thought was to create a ColorScheme class that i will descend from either Outlook2003 or Outlook2007:

public static class ColorScheme : Outlook2007ColorScheme
{
}

but you cannot descend from a static class.

My next thought was to create the static ColorScheme class, but make Outlook2003ColorScheme and Outlook2007ColorScheme classes non-static. Then a static variable in the static ColorScheme class can point to either "true" color scheme:

public static class ColorScheme
{
    private static CustomColorScheme = new Outlook2007ColorScheme();
    ...
}

private class CustomColorScheme 
{ 
   ...
}

private class Outlook2008ColorScheme : CustomColorScheme 
{
    ...
}

private class Outlook2003ColorScheme : CustomColorScheme 
{
   ...
}

but that would require me to convert a class composed entirly of readonly static Colors into overridable properties, and then my ColorScheme class would need to have the 30 different property getters thunk down into the contained object.

That's just too much typing.

So my next thought was to alias the class:

public static ColorScheme = Outlook2007ColorScheme;

But that doesn't compile.

How can i alias a static class into another name?


Update: Can someone please add the answer "You cannot do this in C#", so i can mark that as the accepted answer. Anyone else wanting the answer to the same question will find this question, the accepted answer, and a number of workarounds that might, or might not, be useful.

i just want to close this question out.

+17  A: 

If you change the original class name, you could rewrite the dependent code using an import alias as a typedef substitute:

using ColorScheme = The.Fully.Qualified.Namespace.Outlook2007ColorScheme;

This has to go at the top of the file/namespace, just like regular usings.

I don't know if this is practical in your case, though.

Konrad Rudolph
+3  A: 

try this: using ColorScheme=[fully qualified].Outlook2007ColorScheme

dpurrington
+10  A: 

You want a (Factory|Singleton), depending on your requirements. The premise is to make it so that the client code doesn't have to know which color scheme it is getting. If the color scheme should be application wide, a singleton should be fine. If you may use a different scheme in different circumstances, a Factory pattern is probably the way to go. Either way, when the color scheme needs to change, the code only has to be changed in one place.

public interface ColorScheme {
    Color TitleBar { get; }
    Color Background{ get; }
    ...
}

public static class ColorSchemeFactory {

    private static ColorScheme scheme = new Outlook2007ColorScheme();

    public static ColorScheme GetColorScheme() { //Add applicable arguments
        return scheme;
    }
}

public class Outlook2003ColorScheme: ColorScheme {
   public Color TitleBar {
       get { return Color.LightBlue; }
   }

    public Color Background {
        get { return Color.Gray; }
    }
}

public class Outlook2007ColorScheme: ColorScheme {
   public Color TitleBar {
       get { return Color.Blue; }
   }

    public Color Background {
        get { return Color.White; }
    }
}
Chris Marasti-Georg
This looks like less of a factory and more of a weak attempt at a singleton pattern. A factory would be more likely to parameterize the creation method; you would have something more like: public static ColorScheme GetColorScheme(string descriptor);
OwenP
True - the basic idea is to make sure than when office 2012 comes out, the code only has to change in 1 place.
Chris Marasti-Georg
This definetly works, but it certainly is an enterprise-y solution to a simple problem.
Ian Boyd
I would call it good design.
Chris Marasti-Georg
A: 

Is it possible to change to using an interface?

Perhaps you could create an IColorScheme interface that all of the classes implement?

This would work well with the factory pattern as shown by Chris Marasti-Georg

chills42
It could be, but i'm not going to spend anymore time on it rather than renaming the class that is the "current" color scheme to use.
Ian Boyd
+2  A: 

You can make an alias for your class by adding this line of code at the top of your code:

using Outlook2007ColorScheme = YourNameSpace.ColorScheme;
mnour
+6  A: 

You cannot alias a class name in C#.

There are things you can do that are not aliasing a class name in C#.

But to answer the original question: you cannot alias a class name in C#.

Ian Boyd