为什么我得到,“不能将类型'void'隐式转换为”对象“用这个代码(EPPlus)? [英] Why am I getting, "Cannot implicitly convert type 'void' to 'object'" with this code (EPPlus)?

查看:658
本文介绍了为什么我得到,“不能将类型'void'隐式转换为”对象“用这个代码(EPPlus)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个代码使用EPPlus在电子表格中填充单元格:

  using(var footerMonth1Cell = prodUsageWorksheet.Cells [columnFooterRow,MONTH1_COL])
{
footerMonth1Cell.Value = monthsTruncatedYears [0];
footerMonth1Cell.Style.Font.Size = DATA_FONT_SIZE;
footerMonth1Cell.Style.Horizo​​ntalAlignment = ExcelHorizo​​ntalAlignment.Right;
}

我想,工作正常,但是一个用户抱怨说它正在生产绿色三角形在细胞中。 monthTruncatedYears是一个通用的字符串列表。



所以我添加了这个帮助函数:

  public static void SetValueIntOrStr(此ExcelRangeBase范围,对象值)
{
string strVal = value.ToString();
if(!String.IsNullOrEmpty(strVal))
{
double dVal;
int iVal;

if(double.TryParse(strVal,out dVal))
range.Value = dVal;
else if(Int32.TryParse(strVal,out iVal))
range.Value = iVal;
else
range.Value = strVal;
}
else
range.Value = null;
}

...我从这里,然后尝试重构原始代码来调用帮助函数如下所示:

  using(var footerMonth1Cell = prodUsageWorksheet.Cells [columnFooterRow,MONTH1_COL])
{
footerMonth1Cell.Value = SetValueIntOrStr(footerMonth1Cell,monthsTruncatedYears [0]);
footerMonth1Cell.Style.Font.Size = DATA_FONT_SIZE;
footerMonth1Cell.Style.Horizo​​ntalAlignment = ExcelHorizo​​ntalAlignment.Right;
}

但是,它不会编译,告诉我,不能由于第二个arg传递给SetValueIntOrStr(),即value,是类型对象,因此,将void转换为object


<我认为这是问题。那么为什么编译器看起来将MonthTruncatedYears [0]视为无效?在遗留代码中,我把它分配给单元格的值,当时它不是无效的,所以...?!?



我试过第二个对象就是这样:

  footerMonth1Cell.Value = ReportRunnerConstsAndUtils.SetValueIntOrStr(footerMonth1Cell,object(monthsTuncatedYears [0])); 

...但是也不会用无效的表达式术语对象



...这种类似的尝试:

  footerMonth1Cell.Value = ReportRunnerConstsAndUtils.SetValueDoubleOrIntOrStr(footerMonth1Cell,monthsTruncatedYears [0] as object); 

...引用,无法将类型'void'隐式转换为object



请注意,助手方法实际上在借用的代码中被错误地命名;而不是SetValueIntOrStr,它应该是SetValueDoubleOrIntOrStr

解决方案

问题是线路

  footerMonth1Cell.Value = SetValueIntOrStr(footerMonth1Cell,monthsTruncatedYears [0]); 

SetValueIntOrStr 不返回任何值),因此不能用于将值分配给 footerMonth1Cell.Value



这将是有效的代码,因为Value属性已经在函数内部更改:

  SetValueIntOrStr(footerMonth1Cell,monthsTruncatedYears [0]); 


I was using this code to populate a cell in a spreadsheet using EPPlus:

using (var footerMonth1Cell = prodUsageWorksheet.Cells[columnFooterRow, MONTH1_COL])
{
    footerMonth1Cell.Value = monthsTruncatedYears[0];
    footerMonth1Cell.Style.Font.Size = DATA_FONT_SIZE;
    footerMonth1Cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
}

It was working fine, I thought, but a user complained that it was producing "green triangles" in the cells. "monthsTruncatedYears" is a generic list of string.

So I added this helper function:

public static void SetValueIntOrStr(this ExcelRangeBase range, object value)
{
    string strVal = value.ToString();
    if (!String.IsNullOrEmpty(strVal))
    {
        double dVal;
        int iVal;

        if (double.TryParse(strVal, out dVal))
            range.Value = dVal;
        else if (Int32.TryParse(strVal, out iVal))
            range.Value = iVal;
        else
            range.Value = strVal;
    }
    else
        range.Value = null;
}

...which I got from here, and then tried to refactor the original code to call that helper function like so:

using (var footerMonth1Cell = prodUsageWorksheet.Cells[columnFooterRow, MONTH1_COL])
{
    footerMonth1Cell.Value = SetValueIntOrStr(footerMonth1Cell, monthsTruncatedYears[0]);
    footerMonth1Cell.Style.Font.Size = DATA_FONT_SIZE;
    footerMonth1Cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
}

However, it won't compile, telling me, "Cannot implicitly convert type 'void' to 'object'"

Since the second arg passed to SetValueIntOrStr(), namely "value", is of type object, I assume that is the problem. So why does the compiler apparently view monthsTruncatedYears[0] as being void? In the legacy code I assigned it as the value to the cell, and it wasn't void at that time, so...?!?

I tried casting the second arg to object like so:

footerMonth1Cell.Value = ReportRunnerConstsAndUtils.SetValueIntOrStr(footerMonth1Cell, object(monthsTruncatedYears[0]));

...but that won't compute, either, with "Invalid expression term 'object'"

...and this similar attempt:

footerMonth1Cell.Value = ReportRunnerConstsAndUtils.SetValueDoubleOrIntOrStr(footerMonth1Cell, monthsTruncatedYears[0] as object);

...elicits, "Cannot implicitly convert type 'void' to 'object'"

Note that the helper method is actually misnamed in the borrowed code; instead of "SetValueIntOrStr" it should be "SetValueDoubleOrIntOrStr"

解决方案

The problem is the line

footerMonth1Cell.Value = SetValueIntOrStr(footerMonth1Cell, monthsTruncatedYears[0]);

SetValueIntOrStr does not return any value (void) and therefore cannot be used to assign a value to the footerMonth1Cell.Value.

This would be valid code because the Value property is already changed inside the function:

SetValueIntOrStr(footerMonth1Cell, monthsTruncatedYears[0]);

这篇关于为什么我得到,“不能将类型'void'隐式转换为”对象“用这个代码(EPPlus)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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