tags:

views:

147

answers:

3

I have a string property that I would like to be able to force two things with: - It can only be set to specific vaues within a pre-defined list, - Error checking of the property's value can be performed at compile time.

An enum fits the bill perfectly except that in my list of pre-defined strings there is one with a hyphen and enum values cannot contain hyphens. To illustrate the ideal solution if an enum could contain hyphens I would create an enum of:

public enum SIPEventPackagesEnum
{
    dialog,
    message-summary,
    refer
}

To use:

SIPEventPackagesEnum EventPackage = SIPEventPackagesEnum.message-summary;

To set:

string eventPackageStr = "message-summary";
SIPEventPackagesEnum EventPackage = (SIPEventPackagesEnum)Enum.Parse(typeof(SIPEventPackagesEnum), eventPackageStr, true);

In the above cases it's impossible to set the EventPackage property to anything but one of the enum values and is intuitive to use since intellisense will list the available options.

Due to the inability to use a hyphen, and I cannot change the pre-defined list to remove the hyphen, the very crude approach is to use a struct and have a "Value" property on the struct that does the enforcing in its setter, see below. It's very verbose compared to using an enum and also doesn't allow any compile time checking and isn't very intuitive.

Has anyone encountered this problem before and have a better solution? I have multiple lists with items containing hyphens so it's not a once off.

public struct SIPEventPackages
{
    public const string DIALOG = "dialog";                   
    public const string MESSAGE_SUMMARY = "message-summary";   
    public const string REFER = "refer";                      

    public string Value
    {
        get { return Value; }
        set
        {
            if (IsValid(value))
            {
                Value = value.ToLower();
            }
            else
            {
                throw new ArgumentException(value + " is invalid for a SIP event package.");
            }
        }
    }

    public bool IsValid(string value)
    {
        if (value.IsNullOrBlank())
        {
            return false;
        }
        else if (value.ToLower() == DIALOG || value.ToLower() == MESSAGE_SUMMARY || value.ToLower() == REFER)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public override string ToString()
    {
        return Value;
    }
}
A: 

Seems this is simple using an delegate

        Func<string, bool> isValid = str =>
        {
            List<string> validLst = new List<string>() { "dialog","message-summary","refer" };
            if (validLst.Find(x => string.Equals(x,str,StringComparison.InvariantCultureIgnoreCase)) == null)
                return false;
            return true;

        };

        var teststr1 = "message-summary";
        var teststr2 = "wrongone";
        isValid(teststr1);
        isValid(teststr2);

Uptdate:

Otherwise you can use the enum approach in a little different way. Have a enum value without an hyphen. And just strip the hyphens from your source string when parse the enum values. this will work as you expected

    public enum SIPEventPackagesEnum
    {
        dialog,
        messagesummary,
        refer
    }

    string eventPackageStr = "message-summary";
    SIPEventPackagesEnum EventPackage = (SIPEventPackagesEnum)Enum.Parse(typeof(SIPEventPackagesEnum), eventPackageStr.Replace("-",""), true);

Cheers

Ramesh Vel
A: 

You could create the enum without -, then have a static Dictionary<SIPEventPackagesEnum ,string> in a helper class mapping from enum value to string to convert from enum to string, then use Enum.Parse(typeof(SIPEventPackagesEnum), str.Replace("-", "")) when converting from string to enum.

Or use _ instead of - and replace _ with - and vice versa when required

Or use camel case in the enum values and replace a capital letter within the enum name with "-<lowercase letter>" using a regex

thecoop
That would be the right way to go except that you can't override ToString() on an enum. So when I need to write the enum value, in this case to an XML document, it would be easy to miss doing the conversion.
sipwiz
A: 

I managed to fine tune my approach so that it's almost as good as an enum albeit with a lot more plumbing code required. It's worth the plumbing code to save potential misues problems in the future.

public struct SIPEventPackage
{
    public static SIPEventPackage None = new SIPEventPackage(null);
    public static SIPEventPackage Dialog = new SIPEventPackage("dialog");                   
    public static SIPEventPackage MessageSummary = new SIPEventPackage("message-summary"); 
    public static SIPEventPackage Refer = new SIPEventPackage("refer");                    

    private string m_value;

    private SIPEventPackage(string value)
    {
        m_value = value;
    }

    public override string ToString()
    {
        return m_value;
    }

    public static SIPEventPackage Parse(string value)
    {
        if (!IsValid(value))
        {
            throw new ArgumentException("The value is not valid for a SIPEventPackage.");
        }
        else
        {
            string trimmedValue = value.Trim().ToLower();
            switch (trimmedValue)
            {
                case "dialog": return SIPEventPackage.Dialog;
                case "message-summary": return SIPEventPackage.MessageSummary;
                case "refer": return SIPEventPackage.Refer;
                default: throw new ArgumentException("The value is not valid for a SIPEventPackage.");
            }
        }
    }
}

There's a little bit more plumbing required, implementing an IsValid method and operator == and a few more, but the main thing is I can now use the struct in almost an identical way to an enum and can have items with hyphens.

sipwiz