Solved

using an Array in a Switch Statement

Posted on 2006-06-12
2
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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