如果选项严格设置为关闭,使用方法重载是不是一个坏主意?请参阅下面的代码。 .NET中的每种数据类型都有一个单独的函数。该类可以处理Oracle和SQL服务器参数:
Public Class clsParameterValues
'Implements IDisposable
Private paramValues(0) As DbParameter
Public Function AssignParameterValues(ByVal strParameterName As String, ByVal strParameterValue As Long, ByVal intDatabaseType As Integer) As Integer
Dim intArrayBound As Integer
intArrayBound = UBound(paramValues)
'If intArrayBound > 0 Then
If paramValues(0) Is Nothing = False Then
intArrayBound = intArrayBound + 1
ReDim Preserve paramValues(intArrayBound)
End If
If intDatabaseType = 1 Then
paramValues(intArrayBound) = New SqlParameter(strParameterName, strParameterValue)
ElseIf intDatabaseType = 2 Then
paramValues(intArrayBound) = New OracleParameter(strParameterName, strParameterValue)
'paramValues(intArrayBound) = New OracleParameter(":" & strParameterName, OracleType.Int32)
'paramValues(intArrayBound).Value = strParameterValue
End If
paramValues(intArrayBound).DbType = DbType.Int64
Return intArrayBound
End Function
Public Function AssignParameterValues(ByVal strParameterName As String, ByVal strParameterValue As Integer, ByVal intDatabaseType As Integer) As Integer
Dim intArrayBound As Integer
intArrayBound = UBound(paramValues)
'If intArrayBound > 0 Then
If paramValues(0) Is Nothing = False Then
intArrayBound = intArrayBound + 1
ReDim Preserve paramValues(intArrayBound)
End If
If intDatabaseType = 1 Then
paramValues(intArrayBound) = New SqlParameter(strParameterName, strParameterValue)
ElseIf intDatabaseType = 2 Then
paramValues(intArrayBound) = New OracleParameter(strParameterName, strParameterValue)
'paramValues(intArrayBound) = New OracleParameter(":" & strParameterName, OracleType.Int32)
'paramValues(intArrayBound).Value = strParameterValue
End If
paramValues(intArrayBound).DbType = DbType.Int32
Return intArrayBound
End Function
Public Function AssignParameterValues(ByVal strParameterName As String, ByVal strParameterValue As String, ByVal intDatabaseType As Integer) As Integer
Dim intArrayBound As Integer
intArrayBound = UBound(paramValues)
'If intArrayBound > 0 Then
If paramValues(0) Is Nothing = False Then
intArrayBound = intArrayBound + 1
ReDim Preserve paramValues(intArrayBound)
End If
If intDatabaseType = 1 Then
paramValues(intArrayBound) = New SqlParameter(strParameterName, strParameterValue)
ElseIf intDatabaseType = 2 Then
paramValues(intArrayBound) = New OracleParameter(strParameterName, strParameterValue)
'paramValues(intArrayBound) = New OracleParameter(":" & strParameterName, OracleType.Int32)
'paramValues(intArrayBound).Value = strParameterValue
End If
paramValues(intArrayBound).DbType = DbType.String
Return intArrayBound
End Function
Public Function AssignParameterValues(ByVal strParameterName As String, ByVal strParameterValue As Date, ByVal intDatabaseType As Integer) As Integer
Dim intArrayBound As Integer
intArrayBound = UBound(paramValues)
'If intArrayBound > 0 Then
If paramValues(0) Is Nothing = False Then
intArrayBound = intArrayBound + 1
ReDim Preserve paramValues(intArrayBound)
End If
If intDatabaseType = 1 Then
'paramValues(intArrayBound) = New SqlParameter(strParameterName, DateValue(strParameterValue))
paramValues(intArrayBound) = New SqlParameter(strParameterName, strParameterValue)
ElseIf intDatabaseType = 2 Then
paramValues(intArrayBound) = New OracleParameter(strParameterName, strParameterValue)
'paramValues(intArrayBound) = New OracleParameter(":" & strParameterName, OracleType.Int32)
'paramValues(intArrayBound).Value = strParameterValue
End If
paramValues(intArrayBound).DbType = DbType.Date
Return intArrayBound
End Function
Public Function AssignParameterValues(ByVal strParameterName As String, ByVal strParameterValue As Decimal, ByVal intDatabaseType As Integer) As Integer
Dim intArrayBound As Integer
intArrayBound = UBound(paramValues)
'If intArrayBound > 0 Then
If paramValues(0) Is Nothing = False Then
intArrayBound = intArrayBound + 1
ReDim Preserve paramValues(intArrayBound)
End If
If intDatabaseType = 1 Then
paramValues(intArrayBound) = New SqlParameter(strParameterName, strParameterValue)
ElseIf intDatabaseType = 2 Then
paramValues(intArrayBound) = New OracleParameter(strParameterName, strParameterValue)
'paramValues(intArrayBound) = New OracleParameter(":" & strParameterName, OracleType.Int32)
'paramValues(intArrayBound).Value = strParameterValue
End If
paramValues(intArrayBound).DbType = DbType.Decimal
Return intArrayBound
End Function
Public Function AssignParameterValues(ByVal strParameterName As String, ByVal strParameterValue As Boolean, ByVal intDatabaseType As Integer) As Integer
Dim intArrayBound As Integer
intArrayBound = UBound(paramValues)
'If intArrayBound > 0 Then
If paramValues(0) Is Nothing = False Then
intArrayBound = intArrayBound + 1
ReDim Preserve paramValues(intArrayBound)
End If
If intDatabaseType = 1 Then
paramValues(intArrayBound) = New SqlParameter(strParameterName, strParameterValue)
ElseIf intDatabaseType = 2 Then
paramValues(intArrayBound) = New OracleParameter(strParameterName, strParameterValue)
'paramValues(intArrayBound) = New OracleParameter(":" & strParameterName, OracleType.Int32)
'paramValues(intArrayBound).Value = strParameterValue
End If
paramValues(intArrayBound).DbType = DbType.Boolean
Return intArrayBound
End Function
Public Function getParameterValues() As DbParameter()
Return paramValues
End Function
End Class
调用者有责任确保类型正确。例如,如果要创建date类型的参数,请传递日期类型。这合理吗?
答案 0 :(得分:1)
根据MSDN,Option Strict
在解析重载函数/ subs时不会改变运行时的行为。因此(并且因为你声明你涵盖了每种价值类型)在你的情况下应该无关紧要。
答案 1 :(得分:0)
我认为这绝对是不好的做法。 IMO(你应该总是Option Strict On
)。
但是,只要您根据您可能期望的数据类型覆盖每个案例,那么您的代码将起作用。正如Stephan B所说,Option Strict
不会改变重载方法的解析方式。当您尝试使用一种类型的变量时,它只允许动态装箱/取消装箱和类型转换,就好像它是一种不同的类型(即,如果您尝试在String
上进行数学运算)。