Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 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
 
Old June 22nd, 2010, 11:36 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default Enumerations

I am writing an application which will monitor an API service and report problems to a list of email addresses if there is a problem or something unexpected with the API.

Currently I have some code in my Program.cs which decides what to do based on user input and a series of if statements.

I have been doing some research and have come across a phrase "you shouldn't have any red in your code" which basically is referring to strings.

I want to change my if statements to enumerations currently I have an if else statement like:

Code:
if (command == "start")
{
//Do something
}
I haven't really used my own enumerations before in something I have written and I feel like this will be a slap on the forehead moment after it is explained to me.

Currently I have created a class and declared a public enumeration in it which contains the commands start, stop and help.

So I now need the user to enter something relate it to the enumeration and then call the appropriate method.

So I know I will need a switch statement but it's relating the user input to select the values of the enumeration which is stumping me..
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
 
Old June 23rd, 2010, 02:51 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Will,

User input is typically a string, whether the underlying value looks like string, a number or an enumeration.

So, in order to use it you need to convert it to your enum first. The Enum class has a static Parse method that converts from strings and numbers. So, considering this enum:

Code:
enum CommandType
{
 Start = 0,
 Stop = 1,
 Help = 2
}
You can use Parse like this:
Code:
 
static void Main(string[] args)
{
 string userCommand = Console.ReadLine();
 CommandType commandType = (CommandType)Enum.Parse(typeof(CommandType), userCommand);
 string result = String.Empty;
 switch (commandType)
 {
  case CommandType.Start:
   result = "to start";
   break;
  case CommandType.Stop:
   result = "to stop";
   break;
  case CommandType.Help:
   result = "help";
   break;
 }
 Console.WriteLine("You want " + result);
}
Now users can enter 0, 1, 2 at the console, or Start, Stop and Help and the correct block would be triggered.

Clearly, this needs some more work to deal with unknown input but I hope you get the idea.

.NET 4 comes with TryParse which lets you, well, try the parse operation without raising an exception.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; June 23rd, 2010 at 08:37 AM.. Reason: Spelling
 
Old June 23rd, 2010, 04:02 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Thanks for that.

This is still case sensitive though I want to have the enumerations as Command.Start and not Command.start for example.

All I can think about doing here is some string manipulation to ensure the first character is upper and the rest are converted to lower. Is this common practice for a console application? Or are there better ways of dealing with it?
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
 
Old June 23rd, 2010, 04:22 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Will,
Quote:
This is still case sensitive though
It's not. Check out the documentation or IntelliSense to see the overload of Parse that solves the case issue: http://msdn.microsoft.com/en-us/libr...num.parse.aspx

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 23rd, 2010, 04:38 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Awesome, thanks a lot. I shall use my eyes a bit harder next time.
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
 
Old June 23rd, 2010, 07:59 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Enums

Hi Will:

Imar has given you the solution you requested. I thought I'd just add a few extra things to think about.

First, as Imar mentions, TryParse() can be used without fear of an exception blowing your code out of the water. It's a little more safe in that it traps inputs that might throw an exception.

Second, while it's not an issue here, don't forget that string variables have methods for case conversion. For example: str = txtMyInput.Text.ToUpper(); This converts all inputs to upper case, which means you can make case assumptions during subsequent processing. If you look closely at many database applications, they convert everything to upper case when the data are saved to the database so that subsequent queries don't have to worry about case.

Third, and this is just a personal preference, I always put a default statement in any switch block. That way, if your user enters a value that doesn't match a case statement, at least you have a chance to do something before marching over the cliff.

Fourth, you could use constants instead of enum's if you wanted to. See page 448 for an example. In your case, I'd prefer the constants because I can't directly use an enum for index into a string. For example:
Code:
enum mine
{
one,
two, three
};
staticstring[] mystr = newstring[]{"one", "two", "three"};
and then trying:

 
string tmp = mystr[mine.one];

won't work because enums are not integers and, hence, cannot directly be used to index into an array. For it to work, you'd have to use a cast:

string tmp = mystr[(int) mine.one];

which seems awkward to me. Also, since I didn't use the assignment statements for the enums like Imar did, enum one actually has a (default) value of 0, which is a bug just waiting to happen. Using Imar's approach to explicitly define the enum is the right way to use an enum. The history of bad code is lined with programmers who use shortcuts.

Hope this didn't confuse you...


__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)

Last edited by DrPurdum; June 23rd, 2010 at 08:07 AM.. Reason: to display code in a code box
 
Old June 23rd, 2010, 08:36 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Third, and this is just a personal preference, I always put a default statement in any switch block. That way, if your user enters a value that doesn't match a case statement, at least you have a chance to do something before marching over the cliff.
Couldn't agree with you more. I normally do that as well, and it was (cheesy excuse) more or less what I was referring to with "Clearly, this needs some more work to deal with unknown input but I hope you get the idea".

Agree with the rest you said here too.....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 23rd, 2010, 08:49 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Enums

Hi Imar:

Yep, I knew that's what you had in mind when you added the cautionary note...same for the Parse() method. I just wanted to grab the 2x4 and hit Will between the eyes with it to make sure he uses the default statement in any switch block.

Good to see you monitor and help other readers, too!
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old June 23rd, 2010, 09:21 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Cheers, yup I have a default statement.

Personally I just like enumerations more than constants not sure why but I just prefer the concept of them and the way they appear in the IntelliSense but this is just a personal preference coming from limited experience so maybe this will change with time.

For the moment I feel they suit my purpose well in the application.

On the note of Parse vs TryParse should I take advantage of the TryParse built into the new release of the framework?
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
 
Old June 23rd, 2010, 01:41 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Enums

On the note of Parse vs TryParse should I take advantage of the TryParse built into the new release of the framework?

Probably no reason not to.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Enumerations iansummers VB.NET 3 July 9th, 2007 05:01 PM
Chapter 5: Enumerations Richard Stringfellow BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 3 December 5th, 2006 10:46 PM
Chapter 5: Enumerations Richard Stringfellow Visual Basic 2005 Basics 0 December 2nd, 2006 01:28 PM
Enumerations JAL C# 2 November 20th, 2005 10:49 AM





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