471,598 Members | 1,306 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Going from string to enum?

I'm trying to supply an enum value to an enum property but get a
specified cast is not valid error:

fr.CompareC =
(Comparison)this.cmbCCountComparison.Items[this.cmbCCountComparison.SelectedIndex]

Above errors out. In the fr class is:

public enum Comparison{Equal, Greater, Less};
private Comparison _CComparison = Comparison.Greater;

public Comparison CompareC
{
get{return _CComparison;}
set{_CComparison=value;}
}

The combo box has values:
All
Equal
Greater
Less

In the above scenario, it is returning "Greater". The enums have to
stay in place at this point so what ever the solution is, it will need
to work with the enums. Is there a way to do this without getting into
elaborate lookups? Why doesn't the cast work?

Thanks,
Brett

Feb 10 '06 #1
2 3174
"Brett Romero" <ac*****@cygen.com> wrote in
news:11**********************@z14g2000cwz.googlegr oups.com:
fr.CompareC =
(Comparison)this.cmbCCountComparison.Items[this.cmbCCountComparison.Sel
ectedIndex]


Try using Enum.Parse(type, string). You will have to cast the result:

fr.CompareC = (Comparison)Enum.Parse(...);

-mdb
Feb 10 '06 #2
Why doesn't the cast work?


Does the combobox contain strings? If so, you have to use Enum.Parse
rather than a simple cast.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Anonieko Ramos | last post: by
5 posts views Thread by Barry | last post: by
15 posts views Thread by Fariba | last post: by
11 posts views Thread by Peter Kirk | last post: by

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.