473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Enum.TryParse() substitute

Primitives like Int32 provide a Parse() method , and TryParse() method --
which is very useful.

Enum provides a Parse() method, but not a TryParse() method.

Other than wrapping the Enum.Parse() in an exception handler , can someone
recommend a technique similar to TryParse() ?

Thanks.
May 18 '06 #1
1 5692
John A Grandy wrote:
Primitives like Int32 provide a Parse() method , and TryParse() method --
which is very useful.

Enum provides a Parse() method, but not a TryParse() method.

Other than wrapping the Enum.Parse() in an exception handler , can someone
recommend a technique similar to TryParse() ?

Thanks.

Messy but:
int EnumValue;
If(Int.TryParse("1"))
{
MyEnum x = (MyEnum)Enum.Parse(EnumValue);
}

This requires EnumValue is a valid value for MyEnum otherwise it will
get a invalidcastexception.
Wrong it appears, it will work fine but x will still not be a value of
MyEnum.

JB
May 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Douglas | last post by:
Sorry for the re-post, ISP troubles grr... anyways... Gday All, I am developing a DLL (using VB6) for use in an IIS5 ASP Application. This DLL contains a series of 'Public Enum'(s)
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
3
by: Shimon Sim | last post by:
I am writing general method that would take Type that should represent a enum that do something with it. In the middle of the method I need to cast it to this enum. I tried Convert.ChangeType...
8
by: cadilhac | last post by:
Hi, I have the following code: public enum MyColors { Red, Green, Blue } MyColors c = (MyColors)Enum.Parse(typeof(MyColors), "abcd"); Why do I get c = "abcd" after this code instead of...
6
by: Brian Haynes | last post by:
I've read all the posts in this forum that I can find that look related to this issue and I have only found 1 solution that I consider to be a bit of a hack. What I want to do is assign a value to...
5
by: Greg Wilkerson | last post by:
Ok, Someone tell me what I'm doing wrong. The statement below is returning 527.0 in v, instead of the expected 215. Am I missing something? double v; if (Double.TryParse("0d7",...
1
by: Joachim | last post by:
When I run double.TryParse("12.01234567890123456789", out value) I only get 12.0123456789012 as result. How can I get double.TryParse to translate more than 13 decimals (if possible)?
5
by: Steffan A. Cline | last post by:
When using this as indicated and trying to set it to accept multiple formats, it bombs out with the error: Overload resolution failed because no accessible 'TryParse' accepts this number of...
2
by: steven | last post by:
Hi Anybody know why there's no TryParse method for a string type? I can do the following when trying to convert an object to int: object oTest = 1; int iValue; int.TryParse(oTest, iValue); ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.