-4

I've started a Uni course and I can't get me head around how to use my array for a switch case. Basically I just need the help with the switch-case, then I can get on with my work. Heres what it looks like so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Assignment2
{
 class Program
  {
    public const int noOfentries = 6;
    public const int address = 5;
    public static string[,] addressBook = new string[noOfentries, address];//
    string array for the address book
    public static int deletion;
    public static int choice;
    public static ConsoleKeyInfo keyPressed;
    public static short curItem = 0, c;
    public static string[,] menuItems = new string[,]
    { 
        {"Add Entry"},
        {"Delete Entry"},
        {"Print Book to Screen"},
        {"Edit Contact"}, 
        {"Exit"} 
    };


    #region addEntry
    #endregion
    #region deleteEntry
    #endregion
    #region seeBook
    #endregion
    public static void fourthChoice()
    {
        Console.WriteLine("Would you like to edit the name or address?");

    }

    public static void menu()
    { 
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine(menuItems[i,0].PadRight(10));
            Console.Clear();
            for (c = 0; c < menuItems.Length; c++)
            {
                if (curItem == c)
                {
                    Console.Write(">");
                    Console.WriteLine(menuItems[c,0]);
                    Console.ForegroundColor = ConsoleColor.Green;
                }

                else
                {
                    Console.WriteLine(menuItems[c,0]);
                }
            }
            Console.WriteLine("Please select an option with the Arrow Keys");
         }
    }
    public static void entries()
    {
        switch (menuItems[0,0])
        {
            case "Add Entry":
                break;
            case "Delete Entry":
                break;
            case "Print Book to Screen":
                break;
            case "Edit Contact":
                break;
            case "Exit":
                break;

        }
    }
3
  • and what problem did you encounter? Have you actually tried it? Nov 30, 2011 at 13:40
  • What is the exact problem with the switch case statement, what are you trying to do? Nov 30, 2011 at 13:42
  • Well im doing a project for uni that is an address book, we have three seperate stages. Im trying to go ahead in stage 2 by using the arrow keys to select the Add, edit, delete and exit options. It might have worked but i cant go into the option by pressing enter, Ive got the code Else if (Key.KeyChar.ToString() == "Enter") code Nov 30, 2011 at 14:06

2 Answers 2

3

There are two aspects to a switch/case:

  • The value you're switching on
  • The cases you want to branch to

The fact that you're getting the value from an array is irrelevant. It's equivalent to:

string value = menuItems[0, 0];
switch (value)
{
}

Your cases are also constant string values, so that's fine too. (There's duplication there leading to fragile code which you may want to address, but that's a separate matter.)

... and that's fine. It's not really clear what problem you're having at the moment, although it's also unclear why you've got a rectangular array at all, given that you've only got a single "column" per row. Why not just:

public static string[] menuItems = new string[]
{ 
    "Add Entry",
    "Delete Entry",
    "Print Book to Screen",
    "Edit Contact", 
    "Exit"
};

(Leaving aside naming, accessibility, mutability etc.)

3
  • Cheers Jon, I think i will re-do this, I used the Rectangle array becasue i saw it on the internet and they used switch case with it. Nov 30, 2011 at 13:45
  • 2
    @user1073429: It's really important to understand the code you're writing. It can be tricky with advanced APIs etc, but I would encourage you to learn C# as a language thoroughly - before you "accept" any sample code that you find elsewhere, understand how it works and see if there are alternatives which would work better in your situation.
    – Jon Skeet
    Nov 30, 2011 at 13:47
  • That would be good, it should really be the teacher at my uni as this is my first year. It would be nice to learn it from another person. Nov 30, 2011 at 13:52
3

You should use an Enum for that:

private Enum myEnum { Add, Delete, Edit }

void main(myEnum state)
{
    switch (state)
    {
       Add: //do things
            break;
       Edit: //do things
            break;
       Delete: //do things
               break;
    }
}
7
  • I need the text to display, is there a way i can link the enums to the text? Nov 30, 2011 at 13:46
  • here: stackoverflow.com/questions/1167361/… this shows how you can convert enums to an array or list
    – hcb
    Nov 30, 2011 at 13:49
  • Ive tried the code above and i cant get the add function in the switch statement? Nov 30, 2011 at 14:01
  • if you have the enum, just type "switch (state)" and press the TAB key twice. Then you'll see it's "myEnum.Add" instead of just "Add"
    – hcb
    Nov 30, 2011 at 14:31
  • wait, it's "switch", then double TAB, then add the enum variable (like state) in the generated code, then "arrow down" and it automatically generates all enum values as cases.
    – hcb
    Nov 30, 2011 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.