Solved

using an Array in a Switch Statement

Posted on 2006-06-12
2
274 Views
Last Modified: 2010-04-16
I have this code:
        string strSessionPageID = Session["SessionPageID"].ToString();

        switch (strSessionPageID)
        {
            case "3":
                //do x
                break;
            case "5":
                //do y
                break;
        }

what I'd like to be able to do is set up arrays, and use the switch like this:

        string strSessionPageID = Session["SessionPageID"].ToString();

        array xArr = array("3","43","56","68");
        array yArr = array("34","42","51","63");

        switch (strSessionPageID)
        {
            case xArr:
                //do x
                break;
            case yArr:
                //do y
                break;
        }

where I use the ID in the switch, and check it against the arrays to determine the case to perform.

Is there a way to do this, and if so, might I have an example?
0
Comment
Question by:Paul Kahl
2 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 16887293
If your arrays are static (i.e. can be hard-coded):

  switch (strSessionPageID)
  {
    case "3":
    case "43":
    case "56":
    case "68":
      {
        //do x
        break;
      }
    case "34":
    case "42":
    case "51":
    case "63":
      {
        //do x
        break;
      }
    default:
      {
        break;
      }
  }

If your arrays are dynamic (i.e. determined at run-time), you can do something similar to the following.  You would likely use an ArrayList or List<string> or even a SortedList to store your reference x and y values.

  string[] xArr = new string[] { "3", "43", "56", "68" };
  string[] yArr = new string[] { "34", "42", "51", "63" };

  foreach (string x in xArr)
    if (strSessionPageID == x)
    {
      //do x
      break;
    }

  foreach (string y in yArr)
    if (strSessionPageID == y)
    {
      //do y
      break;
    }
0
 
LVL 3

Author Comment

by:Paul Kahl
ID: 16887448
That looks like it will work fantastically well. Thank you very much.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now