2013-12-10 2 views
-1

option strict가 off로 설정된 경우 메서드 오버로딩을 사용하는 것은 나쁜 생각입니까? 아래 코드를 참조하십시오. .NET에는 각 데이터 유형에 대해 별도의 함수가 있습니다. 이 클래스는 Oracle 및 SQL 서버 매개 변수를 처리 할 수 ​​있습니다.Option Strict Off로 함수 오버로딩

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 유형의 매개 변수를 만들려면 날짜 유형을 전달하십시오. 이게 합리적입니까?

답변

1

MSDN에 따르면 Option Strict은 오버로드 된 기능/서브를 해결할 때 런타임의 동작을 변경하지 않습니다. 따라서 (귀하가 모든 가치 유형을 다룬다는 이유로) 귀하의 경우에는 중요하지 않습니다.

0

분명히 나쁜 습관이라고 생각합니다. IMO (항상 Option Strict On이어야합니다).

그러나 예상되는 모든 데이터 유형을 다루는 한, 코드가 작동합니다. Stephan B가 말했듯이 Option Strict은 오버로드 된 메서드가 어떻게 처리되는지를 변경하지 않습니다. 한 유형의 변수를 다른 유형 인 것처럼 사용할 때 (즉, String에서 수학을 시도하는 경우) 즉석에서 복싱/언 박싱 및 유형 변환을 허용합니다.

관련 문제