Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8115403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:28:32+00:00 2026-06-06T03:28:32+00:00

My code is as follows. Basically, I am reading an excel file and storing

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:28:33+00:00Added an answer on June 6, 2026 at 3:28 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 different jquery uses & put it into one code as follows:
So basically my problem can be written in pseudo-code as follows: split the line
I am reading through a file using a batch script. Basically I want to
I have a lot of boilerplate code that basically follows this pattern: function doSomething()
Basically I have code which is follows: $('selector').on('click',function(){ //Do something here }); $('selector').click(function(){ //Does
Getting this error when I follow the simple code example from jsoncpp, that basically
My code follows public static void main(String[] args) { try { PdfReader reader =
Why can't I do something like this in c# (pseudo code follows) Interface1 {
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
I have code as follows: $(#item_select).change(function() { var params = $(#item_select option:selected).val(); $.post('/account/ar_form.php', {idata:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.