Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > C# 2012/5.0 General Discussion
| Search | Today's Posts | Mark Forums Read
C# 2012/5.0 General Discussion Discussions about the C# 2012 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2012/5.0 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 1st, 2013, 08:12 PM
Registered User
Points: 14, Level: 1
Points: 14, Level: 1 Points: 14, Level: 1 Points: 14, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2012
Location: NYC, NY
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Post Converting a string to an enumeration value.

In the Beginning Visual C# 2012 Programming book, page 90, it is written that it is possible to convert a string to an enumeration value.

That statement is a little obscure. Firstly, what does it mean to convert a string into an enumeration value. Second, why would you want to do it, under what circumstances. (Example?)

Thanks
  #2 (permalink)  
Old January 4th, 2013, 08:20 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi jllouis,

Even tho you write an enumeration value in code kind of looking like a string and they can appear the same when printed to the screen, strings and enumerations are two different types of object and behave in different ways. In fact an enumeration is really a number that can conveniently be used in code using text.

So enum Color.Red cannot be used the same way as string "Red".

A common reason why you might want to convert from one to the other could be to read configuration values from a file to use in a program.

For example, a windows application may read in a config value to set the background color of the form:

Code:
// read string from app.config text file ("Red")
string backColorString = ConfigurationManager.AppSettings["FormBackground"];

// convert to a Color Enum (Color.Red)
System.Drawing.Color backColorEnum = (System.Drawing.Color)Enum.Parse(
    typeof(System.Drawing.Color), backColorString
);

// the BackColor property is of type Enum Color, so use our converted value
MyForm.BackColor = backColorEnum;

// this will error - you cant set the BackColor to a string value
MyForm.BackColor = backColorString;


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting string concatenations FOD C# 4.0 aka C# 2010 General Discussion 5 May 1st, 2014 01:58 AM
Converting a string to an expression Nick.Net VB.NET 2002/2003 Basics 1 March 5th, 2004 09:41 PM
Converting String to an integer Louisa VB.NET 2002/2003 Basics 2 March 5th, 2004 10:25 AM
Converting int to string Judex C++ Programming 3 March 1st, 2004 10:44 AM
Converting a string to a number steve456 Javascript How-To 2 November 17th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.