Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would like to know how I can use a switch statement with enum values for the following scenarios:

I am making a small program for a flight reservation system. The program is meant to enter certain details about the passenger. The program also restricts the user to choose destinations other than the following 3 destinations. Once destination is chosen, the flight number is supposed to be automatically assigned. The user will choose one of three destinations and program will assign an appropriate Flight No for the passenger.

The Flight No will be assigned as below:

     Destination                    Flight No.
      London                            201
      Frankfurt                         233
      Berlin                            241

So let's say I make an enum of destinations, then how can I use switch statement here? This isn't a homework. I am doing it just for the sake of exploring.

share|improve this question

3 Answers 3

up vote 9 down vote accepted

Your scenario doesn't sound like an appropriate use for an enumeration. Also, unless the values never change, this should be data driven.

If the values never change, then a key-value pair is more appropriate where the destination is the key and the flight number is the value. This would eliminate any need for a switch statement, as the key can be used to directly and efficiently locate the value.

if you really want to use an enum, then the switch statement in C# would be something like:

int flightNumber;
Destination selection = // some value collected from user input;

switch( selection )
{
    case Destination.London:
        flightNumber = 201;
    break;

    // etc.
}
share|improve this answer
    
And here Destination is simply an enum i.e. enum Destination {London, Frankfurt, Berlin}...right? –  Maxood Dec 24 '11 at 9:36
1  
@Maxood - correct. Alternatively, you could make the enum value equivalent to the flight number (London = 201, Frankfurt = 233, etc) and you wouldn't need a switch statement then. As I said in my answer, I don't view this as a correct use of an enumeration unless you are absolutely sure the values will never change nor will the list grow. Also, enums are intended to abstract their underlying constant values, not correlate adjacent values. Another programmer looking at your code wouldn't necessarily know that your enum values represented flight numbers. –  Tim Medora Dec 24 '11 at 9:44
    
By the way in C#, you can assign int to each enumeration value: enum Destination {London=201, ...} and get the value by doing int number=(int) Destination.London. –  SRKX Dec 24 '11 at 9:45
1  
@SRKX - correct, that's what I mentioned in my last comment, but I view that as a misuse of the structure for the aforementioned reasons. But it is very useful in legitimate cases (including the declaration of bitwise flags). –  Tim Medora Dec 24 '11 at 9:49
1  
@Maxood - It depends on your input methodology, but essentially you would build your input list from the enumeration and then cast the selected value back to the enumeration. In ASP.NET it would be something like: geekswithblogs.net/jawad/archive/2005/06/24/EnumDropDown.aspx –  Tim Medora Dec 24 '11 at 10:19

I agree with Tim Medora, it is not a proper idea to use an enum in that case, because it forces you to have hardcode your destinations and your flight numbers.

What I would encourage you to do, is to have a of possible destinations List<string> and a mapping between the destinations and the flight number, e.g Dictionnary<string,int> which you generate dynamically at the start of the program according to the content of the database.

share|improve this answer

This is a typical Hash table, isn't it?

  1. It will be much more efficient to use hash table when the number of entries is large, which in your case is very likely.
  2. It is easier to implement the operations on the retrieving, storing and modifying data than your enum-switch implementation.
  3. It's easier to maintain the code. It will be horrible if you have dozens of destinations defined in your enum and switch statements.
share|improve this answer
1  
Can you please expand on this answer. Why do you think a hash table will solve this problem? –  ChrisF Dec 24 '11 at 12:10
    
@ChrisF: I believe he's talking more about a "dictionary". (Perl calls them "hashes", and many languages' most useful and/or efficient implementation of dictionaries is as a hash table). –  cHao Dec 24 '11 at 17:56
    
sorry, I thought 'hash table' is already a well known term and everyone knows it, but it turns out that not so many people know its existence. I'm confident to say this is a very efficient way for the OP's problem in terms of software execution time as well as the program design. –  james Dec 24 '11 at 20:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.