Solved

using an Array in a Switch Statement

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…

756 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