Partager via


TemplateParameterCollection.vb

Imports System
Imports System.Data
Imports System.Collections
Imports System.Globalization

Namespace DotNetDataProviderTemplate
  '*
  '* Because IDataParameterCollection is primarily an IList,
  '* the sample can use an existing class for most of the implementation.
  '*
  Public Class TemplateParameterCollection
    Inherits ArrayList
    Implements IDataParameterCollection
    
    Public Overloads Default Property Item(ByVal index As String) As Object Implements IDataParameterCollection.Item
      Get
        Return Me(IndexOf(index))
      End Get
      Set
        Me(IndexOf(index)) = value
      End Set
    End Property

    Public Overloads Function Contains(ByVal parameterName As String) As Boolean Implements IDataParameterCollection.Contains
              Return (-1 <> IndexOf(parameterName))
    End Function

    Public Overloads Function IndexOf(ByVal parameterName As String) As Integer Implements IDataParameterCollection.IndexOf
      Dim index As Integer = 0
      Dim item As TemplateParameter

      For Each item in Me 
        If 0 = _cultureAwareCompare(item.ParameterName, parameterName) Then
          Return index
        End If
        index += 1
      Next
      Return -1
    End Function

    Public Overloads Sub RemoveAt(parameterName As String) Implements IDataParameterCollection.RemoveAt
      RemoveAt(IndexOf(parameterName))
    End Sub

    Public Overrides Overloads Function Add(value As Object) As Integer
      Return Add(CType(value, TemplateParameter))
    End Function

    Public Overloads Function Add(value As TemplateParameter) As Integer
      If CType(value, TemplateParameter).ParameterName <> Nothing Then
        Return MyBase.Add(value)
      Else
        Throw New ArgumentException("parameter must be named")
      End If
    End Function

    Public Overloads Function Add(parameterName As String, type As DbType) As Integer
      Return Add(new TemplateParameter(parameterName, type))
    End Function

    Public Overloads Function Add(parameterName As String, value As Object) As Integer
      Return Add(new TemplateParameter(parameterName, value))
    End Function

    Public Overloads Function Add(parameterName As String, dbType As DbType, sourceColumn As String) As Integer
      Return Add(New TemplateParameter(parameterName, dbType, sourceColumn))
    End Function

    Private Function _cultureAwareCompare(ByVal strA As String, ByVal strB As String) As Integer
      Return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreKanaType Or CompareOptions.IgnoreWidth Or CompareOptions.IgnoreCase)
    End Function
  End Class
End Namespace