Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > C# 2012/5.0 General Discussion
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Reply With Quote
  #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;
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:52 PM.


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