Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 640167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:55:25+00:00 2026-05-13T20:55:25+00:00

I have a string property that I would like to be able to force

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:55:25+00:00Added an answer on May 13, 2026 at 8:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a property: public list<String> names { get; set; } How can
Hey. I have an object that has a string property called BackgroundColor. This string
I have two Spring proxies set up: <bean id=simpleBean class=org.springframework.aop.framework.ProxyFactoryBean> <property name=target> <ref local=simpleBeanTarget/>
I have a class with a string property, having both a getter and a
I have a List of Foo. Foo has a string property named Bar. I'd
Imagine I have a property defined in global.asax. public List<string> Roles { get {
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have string that I want to chop to array of substrings of given
I would like to use an objects property as the key for a dictionary.
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.