-9

My code is as follows. Basically, I am reading an excel file and storing its contents into an object array. Then, I use a switch case statement to do different operations. Check my code below:-

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
namespace Excel1
{
    class Program
    {
        public static void Main(string[] args)
        //public void ExcelOps()
        {
            //string str;
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/WebServiceTemplate.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;
            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;
            int numSheets = xlWorkbook.Sheets.Count;
            //
            // Iterate through the sheets. They are indexed starting at 1.
            //
            for (int sheetNum = 1; sheetNum <=1; sheetNum++)
            {
                Worksheet sheet = (Worksheet)xlWorkbook.Sheets[sheetNum];
                //
                // Take the used range of the sheet. Finally, get an object array of all
                // of the cells in the sheet (their values). 
                //
                object[,] valueArray = (object[,])xlRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);

                //
                // Do something with the data in the array with a custom method.
                //                
                ProcessInput(valueArray);
            }
        }
        public static void ProcessInput(object[,] valueArray)
        {
            foreach (var value in valueArray)
            {
                switch ((string)value.ToString())
                {
                    case "ITemplate.GetAllTemplate":
                        {
                            //ITemplate.GetAllTemplate
                            break;
                        }
                    case "ITask.GetTaskInstanceFromTemplate":
                        {
                            //ITask.GetTaskInstanceFromTemplate
                            break;
                        }
                    case "CreateTask":
                        {
                            //CreateTask
                            break;
                        }
                    case "UpdateDatabase":
                        {
                            //UpdateDatabase
                            break;
                        }
                    case "GetTaskStatus":
                        {
                            //GetTaskStatus
                            break;
                        }
                    case "VerifyValue":
                        {
                            //VerifyValue
                        }
                        break;
                }
            }
        }
    }
}

When I build it, I get an error Object reference not set to an instance of an object.
the error appears in the switch statement

Can someone help me with this?

3
  • Hi. You must pass array element but not whole array. Commented Jun 15, 2012 at 8:51
  • Have you even tried looking at least MSDN for switch case ? Commented Jun 15, 2012 at 8:52
  • 1
    And the comments in the cases are the same of the values in excel cells in this question by Harish Kumar... Commented Jun 15, 2012 at 9:04

4 Answers 4

6

The error message refers to the type of the variable in the brackets after your switch. In your case, this is an array, so obviously not (as the error message says) a bool, char, string, integral, enum, or corresponding nullable type.

Sign up to request clarification or add additional context in comments.

Comments

4

valueArray is a multidimensional array of objects, according to your parameter definition. switch does not support that.

  1. You can't and wouldn't perform a switch on an array of values; switch operates on a single value. You can use foreach to flatten the array, iterate over each value, and apply the switch, however...
  2. As the error indicates, object cannot be used in switch statements, only types those indicated in the error message. If each value is an integer, cast the value to an int in the switch.

Update OK, now they are strings again.

Example:

foreach(var value in valueArray)
{
    switch(value as string)
    {
        case "ITemplate.GetAllTemplate":
                    break;
        case "ITask.GetTaskInstanceFromTemplate":
                    break;
        case "CreateTask":
                    break;
        case "UpdateDatabase":
                    break;
        case "GetTaskStatus":
                    break;
        case "VerifyValue":
                    break;
    }
}

12 Comments

can u help me as to how v can loop through each dimension and apply d switch?
mine contains only characters like words...so?
Just cast to whatever the actual type is. "characters like words"; so cast to string instead of int and change cases to "1" instead of 1 (or whatever the actual values are that you are interested in).
Hi..i did what u suggested...but i get this error "Unable to cast object of type 'System.Double' to type 'System.String'." The error is coming in the switch statement..any fix pls ?
OK, so the value of each item is Double. Cast to that instead.
|
2

It is quite self explaining: you can't use the switch statement with valueArray object because of its type (object[,]).

Comments

2

Arrays are not supported by Switch. It takes a single argument and Cases should be defined according to it.

Ex:

     int num=3;
     switch(num)  //in case of integer type
         Case 1:
         Case 2:
         ...
     }


     char ch='a';
     switch(ch)  //in case of character type
     {
         Case 'a':
         Case 'b':
         Case '/':
         ...
     }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.