Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

using an Array in a Switch Statement

Posted on 2006-06-12
2
Medium Priority
?
289 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 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