0

I want to know where is the location each data in the array, index number of the data and also the value. I try to use switch case but he problem keep popping up "Control cannot fall through from one case label ('case1:') to another". I think because I used switch case inside of another switch case block. Help me to know if it is possible to have switch case inside of another switch case. If it possible why my code is error? If No, Can you suggest me another way to give the output that i want. I am very pleasure to accept your comments and suggestion.

    //Multi-dimensional Array
int i, c = 0;
string[,] custNames = new string[2, 2] { { "Bob", "Smith" }, { "Sally", "Marks" } };
            Console.WriteLine(custNames.Length);
 for (i = 0; i < custNames.Length; i++)
 {
  switch(i)
  {
      case 0:
           switch (c)
           {
               case 0:
                   c = 0;
                   Console.WriteLine("Array {0} : Value : {1}", i, 
                   custNames[i, c]);
                   continue;
               case 1:
                   c = 1;
                   Console.WriteLine("Array {0} : Value : {1}", i, 
                   custNames[i, c]);
                   continue;
               default:
                   break;                          
            }
      case 1:
           for (c = 0; c < custNames.Length; c++)
           {
            switch (c)
            {
                case 0:
                     Console.WriteLine("Array {0} : Value : {1}", i, 
                     custNames[i, c]);
                     continue;
                case 1:
                     Console.WriteLine("Array {0} : Value : {1}", i, 
                     custNames[i, c]);
                     continue;
                default:
                     break;
             }
            }
       default:
           break;
   } 
 }

enter image description here

1 Answers1

0

"Can you suggest me another way"

When you have { FirstName, LastName } data then you should think of a class Person { ... } rather than a multidimensional array.

But I assume you want to practice arrays here. I don't see why you would need a switch at all, the trick is GetLength(dimension):

for (int i = 0; i < custNames.GetLength(0); i++)
{
   for (int j = 0; j < custNames.GetLength(1); j++)
   {
        Console.WriteLine("Array [{0},{1}] : Value : {2}", i, j, custNames[i, j]);
   }
}
H H
  • 263,252
  • 30
  • 330
  • 514
  • Yes exactly. I'm practicing multidimensional arrays. Thank you so much! I got it. I don't know how to use that GetLength. Now i knew it that it use to get the length of the specific row. Good job.! Thank you. Sorry I am novice in programming. :) – Francis Bongoyan Jul 21 '19 at 09:26