0

将代码从 .Net 2.0 转换为 .Net 4.5 时,我收到此错误消息:

选项严格禁止在将“ByRef”参数“ParamValue”的值复制回匹配参数时从“对象”类型缩小到“字符串”类型。

代码如下所示:

Public Shared Function TheFunction(ByRef x As Object ) As Integer
    TheFunction = 5
    // ultimately called like this: SqlCommand.Parameters.AddWithValue("field", x)
End Function

Private Function AFunction(ByVal x As String) As Boolean

   Dim cnt As Integer = TheFunction(x)

End Function

我已经用谷歌搜索了答案,似乎建议是更改TheFunction.

我受限于我无法改变TheFunction

我可以关闭严格,但我宁愿为这个问题提供一个很好的解决方案,比如将 x 复制到另一个变量并将该变量传入。

4

1 回答 1

1

这行得通吗?

Dim boxedObject as Object = CType(x, Object)
Dim cnt As Integer = TheFunction(boxedObject)
x = CType(boxedObject, String)
于 2017-12-31T02:04:25.233 回答