views:

189

answers:

4

SO Commmunity

I have come across a 'problem' that I think must be quite common and wondered if anybody could help.

I am building a simple windows form (using VB.NET) for a friend to use at work. His company has about 10 specific colors schemes (they have list of RGB values) that they use for the company logo, website etc.

I want to follow this color scheme in my application and, to simplify development, would like to build an enumeration of these colors to avoid hard coding the RBG value for every label, panel etc.

My initial thought was to do the following:

Enum ColorTypes
   CompanyDarkBlue = Color.FromArgB(0,56,147)
   CompanyBlue = Color.FromArgB(0,111,198)
   CompanyLightBlue = Color.FromArgB(0,145,201)
End Enum

However, it's not that simple as a constant is required. I looked around on the internet and I found an example of how to achieve what I need but it seemed inordinately complicated for what seems like quite a common requirement for application development.

What do you think is the best way to solve this problem?

Thanks much

A: 

You can store the same info in a class with shared fields/properties and reach the same effect as with an enumeration.

Dont see the point why an enumeration is needed here at all

Henri
+1  A: 

You can use an extension method. First simplify your enum:

enum ColorType
{
   CompanyDarkBlue,
   CompanyBlue,
   CompanyLightBlue
}

The extension method will look something like this:

public static class ColorTypeExtensions
{
   public static Color ToColor(this ColorType colorType)
   {
      switch(colorType)
      {
         case ColorType.CompanyDarkBlue: return Color.FromArgB(0,56,147);
         ...
      }
   }
}

This will allow you to write:

ColorType.CompanyDarkBlue.ToColor();

For more information take a look at C#: Enhance Enums using Extension Methods

BengtBe
+1  A: 

Put these colors in separate class and put them as properties rather than Enumeration.

    public class myColor
{
    public static Color CompanyDarkBlue
    {
        get { return Color.FromArgb(0, 56, 147); }
    }

    public static Color CompanyBlue
    {
        get { return Color.FromArgb(0, 111, 198); }
    }

    public static Color CompanyLightBlue
    {
        get { return Color.FromArgb(0, 145, 201); }
    }
}
Wahid Bitar
Wahid - Thanks. I used this and evrything workd out fine. Thanks for taking the time to help.
+3  A: 

I wouldn't say you need an enumeration here. When you mentioned "color scheme", the usual way to do it is to use polymorphism to allow for different color schemes.

That means you would have your interface like this:

   public interface IColorScheme
   {
        Color CompanyDarkBlue { get; }
        Color CompanyBlue { get; }
        Color CompanyLightBlue { get; }
   }

and then implement it in any way you like it:

   public class MyColorScheme : IColorScheme
   {
        public virtual Color CompanyDarkBlue
        { 
           get { return Color.FromArgB(0,56,147); }
        }

        public virtual Color CompanyBlue
        {
           get { return Color.FromArgB(0,56,147); }
        }

        // etc.
   }

Note that I have also made these properties virtual, so that you can easily override only specific colors if you need a slightly different scheme.

To make the most of this abstraction, your program should mostly use IColorScheme properties, without being aware of the actual implementation being used. Proper color scheme should be instantiated separately, during initialization.

Groo
Groo - Thanks for the answer. I used the code from Wahib (see below). As it happens, 'Interfaces' are the next chapter in the book I am reading and am not familiar with them yet. Once I get my head around them I'll try your suggestion as a learning exercise
Good, keep going! When you get to that point, I would recommend an exercise: try making an app which would change entire color scheme based on user settings, and then see how interfaces can help you do it.
Groo