处理从类型对象缩小到类型字符串的错误消息

时间:2017-12-31 01:57:25

标签: vb.net option-strict

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

  

Option strict on禁止从类型'object'缩小到type   'string'复制'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复制到另一个变量并传入该变量。

1 个答案:

答案 0 :(得分:1)

这会有用吗?

Dim boxedObject as Object = CType(x, Object)
Dim cnt As Integer = TheFunction(boxedObject)
x = CType(boxedObject, String)