传递数据表中的奇怪问题 [英] Strange problem in passing datatable

查看:54
本文介绍了传递数据表中的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在项目中,ExcelLibrary用于从xls格式的Excel文件中读取数据.我在传递数据表时遇到了一个奇怪的问题.在主程序中,如果我使用这样的代码:

In a project, ExcelLibrary is used to read data from an Excel file in xls format. I have met a strange problem in passing a datatable. In the main program, if I use such code:

ClientXLS ClientNewXLS = new ClientXLS();
ClientNewXLS.ExcelInput(openFile.FileName, dataTable_Excel);



在ClientXLS类别中:



while in the class ClientXLS:

class ClientXLS
{
    public void ExcelInput(string FileOpen, DataTable dataTable_Excel)
    {
        CompoundDocument doc = null;
        Workbook workbook = null;
        if (File.Exists(FileOpen))
        {
            doc = CompoundDocument.Open(FileOpen);

            byte[] bookdata = doc.GetStreamData("Workbook");
            workbook = WorkbookDecoder.Decode(new MemoryStream(bookdata));
            foreach (Worksheet worksheet in workbook.Worksheets)
            {
                if (worksheet.Visible)
                {
                    dataTable_Excel = DataSetHelper.CreateDataTable(worksheet, false);
                }
            }
            dataTable_Excel.Rows[0].Delete();
        }
        if (doc != null)
            doc.Close();
    }
}



主程序中的dataTable_Excel为空,而我发现dataTable_Excel通过调试在ClientXLS类中具有正确的数据.显然,数据表的传递是错误的.

如果我在主程序中使用了这样的代码:



The dataTable_Excel in the main program is null, while I find the dataTable_Excel has got the right data in class ClientXLS by debug. It is obvious that the passing of datatable was wrong.

If I use such code in the main program:

ClientXLS ClientNewXLS = new ClientXLS();
dataTable_Excel = ClientNewXLS.ExcelInput(openFile.FileName);



在ClientXLS类别中:



while in the class ClientXLS:

class ClientXLS
{
    public DataTable ExcelInput(string FileOpen)
    {
        DataTable dataTable_Excel = new DataTable();

        CompoundDocument doc = null;
        Workbook workbook = null;
        if (File.Exists(FileOpen))
        {
            doc = CompoundDocument.Open(FileOpen);

            byte[] bookdata = doc.GetStreamData("Workbook");
            workbook = WorkbookDecoder.Decode(new MemoryStream(bookdata));
            foreach (Worksheet worksheet in workbook.Worksheets)
            {
                if (worksheet.Visible)
                {
                    dataTable_Excel = DataSetHelper.CreateDataTable(worksheet, false);
                }
            }
            dataTable_Excel.Rows[0].Delete();
        }
        if (doc != null)
            doc.Close();

        return dataTable_Excel;
    }
}


一切顺利!我已经挣扎了近一个星期,但没有得到答案.


All runs Ok! I have struggled for nearly a week, but got no answer. Has anyone met such problem before?

推荐答案

当然不起作用.
DataTable dataTable_Excel将作为参数传递,但dataTable_excel为空.如果要在函数中动态创建此引用,则需要像这样通过引用传递它:

Of course it does not work.
DataTable dataTable_Excel is being passed as parameter, but the dataTable_excel is null. If you want to create this reference dynamically in the function you need to pass it by reference like this :

ClientXLS ClientNewXLS = new ClientXLS();
DataTable dataTable_Excel=null;
ClientNewXLS.ExcelInput(openFile.FileName, ref dataTable_Excel);



原因是在没有ref的情况下,传递是通过值完成的.因此,对dataTable_Excel的引用是按值传递的.如果再给它分配一个不同的值,则函数返回时不会注意到它.这基本上就是您正在做的.
所以您混淆了
的概念 按值传递
通过引用传递.
注意:



The reason is that without ref, the passing is being done by value. So the reference to dataTable_Excel is passed by value. If you then assign a different value to it, it won''t be noticed when the function returns. That is basically what you are doing.
So you are mixing up the concept of
pass by value
pass by reference.
note:

ClientNewXLS.ExcelInput(openFile.FileName, out dataTable_Excel);



不对称婚纱

不对称的婚纱

便宜的不对称婚纱

购买不对称婚纱

舞会礼服婚纱





Asymmetrical Wedding Dresses

Asymmetrical Wedding gowns

cheap Asymmetrical Wedding Dresses

buy Asymmetrical Wedding Dresses

Ball Gown Wedding Dresses






这篇关于传递数据表中的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆