Solved

using an Array in a Switch Statement

Posted on 2006-06-12
2
279 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there any online third party tool For Conducting Online Exams like Freelancer site ? 7 42
Media.Imaging 1 24
Problem!!! 8 37
linq, c# 8 22
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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