Click here to Skip to main content
12,304,823 members (64,849 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.3K views
1 bookmarked
Posted

Problem in parsing string value to enum type

, 2 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Today I faced one issue with Enum.TryParse or Enum.Parse method. I was trying to parse string value to Enum but Enum.TryParse/Enum.Parse method was giving always true value for string which is not member of enum. public enum FieldAccessType { ReadOnly = 1, Add = 2, Modify = 4, AddModify = 6 } I want

Today I faced one issue with Enum.TryParse or Enum.Parse method. I was trying to parse string value to Enum but Enum.TryParse/Enum.Parse method was giving always true value for string which is not member of enum.

public enum FieldAccessType
    {
        ReadOnly = 1,
        Add = 2,
        Modify = 4,
        AddModify = 6
    }

I want to convert string value say “30″ to enum type that is not member of enum and conversion shoule be fails but I am always getting true value return enum.TryParse method which is surprising. I don’t know why it is not behaving as per expectation.

FieldAccessType type;
bool res=Enum.TryParse("20",out type );
Assert.IsFalse(res); //test is failing bcoz res is true

I found msdn documentation for Enum.TryParse/Enum.Parse and found following lines:

“If value is the string representation of an integer that does not represent an underlying value of the TEnum enumeration, the method returns an enumeration member whose underlying value is value converted to an integral type. If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of TEnum.”

Below is code which I corrected after:

FieldAccessType fieldAccessType;
                    int intEnumValue;
                    if (Int32.TryParse(Value, out intEnumValue))
                    {
                        if (Enum.IsDefined(typeof (FieldAccessType), intEnumValue))
                            return (FieldAccessType) intEnumValue;
                    }

Enum.IsDefined to veryify that the value you parsed actually exists in this particular enum.

I hope this trick will help you.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Neeraj Kaushik1980
Architect Saxo India Pvt Ltd
India India
• Solution Architect /Sr. Lead Developer with 11+ years of professional experience with more emphasize on .net technology and open for any emerging technologies.
• Strong experience in drafting solutions, stakeholder communications and risk management.
• Proved strong coding and designing skills with agile approach (TDD, XP framework).
• Delivered many projects with involvement from inception to delivery phase.
• Strong experience in high performance, multithreaded, low latency applications.
• Have strong experience in web oriented technologies.
• Playing vital role in technical foundation of projects and delivery.
• Ability to communicate with the business and technical stake holders effectively.
• Strong experience in technologies for Capital Market Domain: Front Office & BackOffice (Algorithm Trading tools, messaging framework, Enterprise bus, integration of FIX APIs and many proprietary trading APIs).
• Functional knowledge of Wealth Management System, Equities, Fixed Income, Derivatives, Forex.
• Practical knowledge of building and practicing agile delivery methodologies (SCRUM & TDD).


Technical Expertise: Solution Architectures, Low Latency applications, Algo/ Properietary Trading Systems, Messaging Framework, Integration via FIX protocol APIs (QuickFix, QuickFix/N), ActiveMQ, MSMQ, C#, WinForms, ASP.NET MVC, Jquery, WCF, Sql Server 2008, TFS, TDD

You may also be interested in...

Comments and Discussions

 
Questiondoes not always work that way Pin
Mark Kruger29-May-12 3:07
memberMark Kruger29-May-12 3:07 
AnswerRe: does not always work that way Pin
Neeraj Kaushik 198029-May-12 4:06
memberNeeraj Kaushik 198029-May-12 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 2 May 2012
Article Copyright 2012 by Neeraj Kaushik1980
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid