Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 504 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enum.Parse(), surely a neater way?

#1
Say I have an enum,

public enum Colours
{
Red,
Blue
}

The only way I can see of parsing them is doing something like:

string colour = "Green";
var col = (Colours)Enum.Parse(typeOf(Colours),colour);

This will throw a [System.ArgumentException][1] because "Green" is not a member of the `Colours` enum.

Now I really hate wrapping code in try/catch's, is there no neater way to do this that doesn't involve me iterating through each `Colours` enum, and doing a string comparison against `colour`?

[1]:

[To see links please register here]

Reply

#2
No, there's no "no-throw" method for this (a la TryParse that some other classes have).

However, you could easily write your own so as to encapsulate the try-catch logic (or the IsDefined check) in one helper method so it doesn't pollute your app code:

public static object TryParse(Type enumType, string value, out bool success)
{
success = Enum.IsDefined(enumType, value);
if (success)
{
return Enum.Parse(enumType, value);
}
return null;
}
Reply

#3
I believe that 4.0 has **[Enum.TryParse][1]**

Otherwise use an [extension method][2]:

public static bool TryParse<T>(this Enum theEnum, string valueToParse, out T returnValue)
{
returnValue = default(T);
int intEnumValue;
if (Int32.TryParse(valueToParse, out intEnumValue))
{
if (Enum.IsDefined(typeof(T), intEnumValue))
{
returnValue = (T)(object)intEnumValue;
return true;
}
}
return false;
}

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
Use [`Enum.IsDefined()`][1] first, to save yourself from wrapping in a try/catch. It will return a boolean value of whether or not the input is a valid member of that enum.

[1]:

[To see links please register here]

Reply

#5
If I'm parsing a "*trusted*" enum, then I use Enum.Parse().<br/>
By "*trusted*" I mean, I know it will ALWAYS be a valid enum without ever erroring... ever!

But there are times when "*you never know what you're gonna get*", and for those times, you need to use a nullable return value. Since .net doesn't offer this baked in, you can roll your own. Here's my recipe:

public static TEnum? ParseEnum<TEnum>(string sEnumValue) where TEnum : struct
{
TEnum eTemp;
TEnum? eReturn = null;
if (Enum.TryParse<TEnum>(sEnumValue, out eTemp) == true)
eReturn = eTemp;
return eReturn;
}

To use this method, call it like so:

eColor? SelectedColor = ParseEnum<eColor>("Red");

Just add this method to a class you use to store your other commonly used utility functions.
Reply

#6
Just to expand on Sky's link to the [.Net 4 Enum.TryParse<>](

[To see links please register here]

), viz

Enum.TryParse<TEnum>(
string value,
[bool ignoreCase,]
out TEnum result
)

This can be used as follows:

enum Colour
{
Red,
Blue
}

private void ParseColours()
{
Colour aColour;

// IMO using the actual enum type is intuitive, but Resharper gives
// "Access to a static member of a type via a derived type"
if (Colour.TryParse("RED", true, out aColour))
{
// ... success
}

// OR, the compiler can infer the type from the out
if (Enum.TryParse("Red", out aColour))
{
// ... success
}

// OR explicit type specification
// (Resharper: Type argument specification is redundant)
if (Enum.TryParse<Colour>("Red", out aColour))
{
// ... success
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through