Enum.TryParse

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</csharp>" to "</syntaxhighlight>")
(Improved formatting and updated .Net 4.0 support notice.)
Line 1: Line 1:
[[Category: Extension Scripts]]
+
'''Notice: The latest version of Unity supports .Net 4.0 which provides a builtin [https://docs.microsoft.com/en-us/dotnet/api/system.enum.tryparse?view=netframework-4.7.2#System_Enum_TryParse__1_System_String_System_Boolean___0__ Enum.TryParse] method.'''
[[Category: C Sharp]]
+
  
Author: [[User:Jmpp|Juan Manuel Palacios]]
 
  
==Overview==
+
This script provides a TryParse method for the .Net (3.0 or earlier) System.Enum class, analogous to the non-exception-throwing TryParse methods that other .Net built-in types provide. However this extension method is an instance method, due to the very nature of C# extensions.
This script provides an instance TryParse method for .Net's System.Enum class, analogous to the non-exception-throwing TryParse methods that other .Net built-in types provide. As opposed to those, though, this extension method is an instance method, due to the very nature of C# extension methods themselves.
+
 
+
Note that .Net 4.0 does provide an out-of-the-box Enum.TryParse method, but that's currently not available to Unity users as of its 3.4.2 release.
+
 
+
==Usage==
+
Put the EnumExtensions.cs script in the Extensions folder in your Unity project and use the resulting instance TryParse method according to the code sample below.
+
 
+
 
+
==Sample==
+
<syntaxhighlight lang="csharp">SomeEnum parsedState;
+
if (parsedState.TryParse<SomeEnum>(aString, out parsedState))
+
{
+
    /* The parsed string matches an enum state, proceed successfully here. */
+
}
+
else
+
{
+
    /* The parsed  string does not match any enum state, handle failure here. */
+
}
+
</syntaxhighlight>
+
  
==EnumExtensions.cs==
 
 
<syntaxhighlight lang="csharp">using UnityEngine;
 
<syntaxhighlight lang="csharp">using UnityEngine;
 
using System;
 
using System;
 
using System.ComponentModel;
 
using System.ComponentModel;
 
  
 
public static class EnumExtensions
 
public static class EnumExtensions
Line 45: Line 22:
 
         return false;
 
         return false;
 
     }
 
     }
   
 
   
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
==Usage==
 +
Put the EnumExtensions.cs script in the Extensions folder in your Unity project and use the resulting instance TryParse method according to the code sample below.
 +
 +
<syntaxhighlight lang="csharp">SomeEnum parsedState;
 +
if (parsedState.TryParse<SomeEnum>(aString, out parsedState))
 +
{
 +
    /* The parsed string matches an enum state, proceed successfully here. */
 +
}
 +
else
 +
{
 +
    /* The parsed  string does not match any enum state, handle failure here. */
 +
}
 +
</syntaxhighlight>
 +
 +
[[Category: C Sharp]]
 +
[[Category: Extensions]]

Revision as of 20:03, 7 February 2019

Notice: The latest version of Unity supports .Net 4.0 which provides a builtin Enum.TryParse method.


This script provides a TryParse method for the .Net (3.0 or earlier) System.Enum class, analogous to the non-exception-throwing TryParse methods that other .Net built-in types provide. However this extension method is an instance method, due to the very nature of C# extensions.

using UnityEngine;
using System;
using System.ComponentModel;
 
public static class EnumExtensions
{
 
    public static bool TryParse<T>(this Enum theEnum, string valueToParse, out T returnValue)
    {
        returnValue = default(T);    
        if (Enum.IsDefined(typeof(T), valueToParse))
        {
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
            returnValue = (T)converter.ConvertFromString(valueToParse);
            return true;
        }
        return false;
    }
}

Usage

Put the EnumExtensions.cs script in the Extensions folder in your Unity project and use the resulting instance TryParse method according to the code sample below.

SomeEnum parsedState;
if (parsedState.TryParse<SomeEnum>(aString, out parsedState))
{
    /* The parsed string matches an enum state, proceed successfully here. */
}
else
{
    /* The parsed  string does not match any enum state, handle failure here. */
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox