2014-11-14 49 views
0

我从单元中读取值,并且返回值未修复。而不是写很多功能对每个返回类型的我试着写一个函数与通用返回类型,但我还是坚持了这一点:如何设置通用返回类型

Public Function GetValueFromCells(Of t)(ByVal CellName As String) As t 
    Return CType(_xlWorksheet.Range(CellName).Value2) 
End Function 

无论我将在小区找什么类型的,我要的值转换到我在函数中设置的返回类型。但我所有的尝试都是徒劳的。如果有人能帮助我完成这个任务,那将是非常棒的。

回答

1

它已经一段时间,因为我感动VB.Net,但你应该能够做到这一点:

Public Function GetValueFromCells(Of t)(ByVal CellName As String) As t 
    Return CType(_xlWorksheet.Range(CellName).Value2, t) 
End Function 
0

我将解决几件事情。

使用更多的描述性泛型类型参数,只声明通用类型并返回它。

Public Function GetValueFromCells(Of TCellValue)(ByVal cellName As String) As TCellValue 
    Dim specificValue As TCellValue = Nothing 
    specificValue = DirectCast(_xlWorksheet.Range(cellName).Value2, TCellValue) 
    Return specificValue 
End Function