What are you trying to do is establish a relationship between a string
"Left" and a constant Left whose label just happens to have the same
sequence of characters as your string. No such relationship exists at the
level of the CRL or the compiler. So traditionally speaking, you are out of
luck.
HOWEVER, .Net does provide you with at least three ways to cheat tradition
and get what you are looking for, though the effort may be more than you had
in mind. I would not recommend any of these methods as a best practice but
you could try following: (See my recommendations at the end.)
1) Use reflection to interrogate the enumeration and find the value of the
enumeration member whose name matches your string. For more information on
using reflection look up the System.Reflection namespace.
2) Use the CRLs ability to execute code contained in a string to evaluate
string.Format("HorizontalAlignment.{0}", tAlign) for its value. Here is an
article on how to do that:
http://www.west-wind.com/presentations/dynamicCode/DynamicCode.htm
3) Lastly, it just so happens that when you serialize an enumerated value to
XML using the XmlSerializer object, it does the work in 1) above for you.
For instance if you declared a variable of type HorizontalAlignment as
follows: HorizontalAlignment MyVar = Left and then serialized MyVar to xml
you would get something like "<MyVar>Left</MyVar>" I never tried to
serialize a single value before and my guess is the XML would be lightly
more complex that my off-the-cuff example but you should get the general
idea. Once you serialized such an XML string and see what the format is,
you should be able to construct one containing a any appropriate enumeration
member name and then deserialize it to get the members value.
So what do I recommend?
1) Store your value as a number and avoid the whole issue.
2) If you must store it in a human readable form, do one of the following:
a) Use a case statement to do your conversion.
b) Store your data in an XML file rather than the registry. Then you
can automatically have the advantage of 3) above. You never have to see the
string representation of your data. The XmlSerializer will take an
enumerated value from you and give you one back (upon serialization and
deserialization respectively). All you ever work with is the enumerated
data type.
--Ken