简体   繁体   中英

How to return a value in a method which is the same value returned in a try?

I am actually programming in C# and I have a problem, I don't know if the method is returning what I want. In fact i am using a try/catch fonction and I am returning a value (cellValue1) in it and at the end of my method, i am returning" ""; ".

What does the method return at the end? I want to return the same value as in the try fonction?

Here is my code:

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace excel
{
    class Class1
    {
        public static void ReadExcel()
        {
           Var log = ConstLog.AreaLog.item1;
           log+= "\n"
           log+= "[REGION]-[read excel] : method read excel started" +"\n"; 
foreach (var files in Directory.GetFiles(@"C:\Users\Lionel84100\Desktop\Excel"))
{
         string filepath = @"C:\Users\Lionel84100\Desktop\Excel\fezfFzfe.xlsx";
            try
            {
                IWorkbook workbook = null;
       FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                if (filepath.IndexOf(".xlsx") > 0)
                {
                    workbook = new XSSFWorkbook(fs);
                }
                else if (filepath.IndexOf(".xls") > 0)
                {
                    workbook = new HSSFWorkbook(fs);
                }

                ISheet sheet = workbook.GetSheet("Formatage_IM");
                if (sheet != null)
                {
                    int rowCount = sheet.LastRowNum;


                    for (int i = 1; i <= rowCount; i++)

                    {
                        IRow curRow = sheet.GetRow(i);

                        string cellValue1 = curRow.GetCell(1).StringCellValue;
                        
                        return cellValue1;
                    }

                }

            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
}
            
          return log;

        }

I have replaced string in the starting of the method by a void but there is an error saying that i can't return a value since the method in a void.

the method returns the value of the first cell in the excel file if the file exists, if no, it returns empty string

A method that does not return void must always return a value, except when it returns with an exception .

public string Foo(int i)
{
    if (i == 0)
    {
        return "Zero";
    }
    // If you remove/comment the following line, the function will not compile.
    return "Not Zero";
}

The same applies when there are try/catch handlers in a function:

public string Foo(int i)
{
    try
    {
        if (i == 0)
        {
            return "Zero";
        }
        // If you remove/comment the following line, the function will not compile.
        return "Not Zero";
    }
    catch (Exception)
    {
        // If you remove the following line, the function will also not compile, 
        // because if you handle an exception in a function, it needs to return a value as well (or rethrow the exception)
        return "Error";
    }
}

On the other hand (as in your example) a function returning void must not return a value. That means that either there must be no argument after a return statement or no return statement at all.

So for your function, you need to change the return type from void to string and make sure you return a value in the other cases as well (or throw an exception if you don't find the value/cell).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM