Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***********************************************************************
- ' Author : Elektro
- ' Modified : 17-December-2015
- ' ***********************************************************************
- #Region " Imports "
- Imports System
- Imports System.Collections.ObjectModel
- Imports System.ComponentModel
- Imports System.Diagnostics
- Imports System.Linq
- #End Region
- #Region " INI Key Collection "
- Namespace Types
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Represents a strongly typed list of <see cref="IniKey"/> that can be accessed by an index.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public NotInheritable Class IniKeyCollection : Inherits Collection(Of IniKey)
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="IniKeyCollection"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Sub New()
- End Sub
- #End Region
- #Region " Indexers "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the <see cref="Inikey"/> that matches the specified key name.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="keyName">
- ''' The key name.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The <see cref="Inikey"/>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Default Public Overloads Property Item(ByVal keyName As String) As IniKey
- <DebuggerStepThrough>
- Get
- Return Me.Find(keyName)
- End Get
- <DebuggerStepThrough>
- Set(ByVal value As IniKey)
- Me(Me.IndexOf(keyName)) = value
- End Set
- End Property
- #End Region
- #Region " Public Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Adds a <see cref="IniKey"/> to the end of the <see cref="IniKeyCollection"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="key">
- ''' The key to add.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="ArgumentException">
- ''' Section already exists.;section
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Shadows Sub Add(ByVal key As IniKey)
- If Me.Contains(key.Name) Then
- Throw New ArgumentException(message:="Key already exists.", paramName:="key")
- Else
- MyBase.Add(key)
- End If
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Adds a <see cref="IniKey"/> to the end of the <see cref="IniKeyCollection"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="name">
- ''' The name of the key to add.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="ArgumentException">
- ''' Key already exists.;section
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Shadows Sub Add(ByVal name As String, ByVal value As String, Optional ByVal comment As String = "")
- Me.Add(New IniKey(name, value, comment))
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Adds the specified keys to the end of the <see cref="IniKeyCollection"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="keys">
- ''' The keys to add.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="ArgumentException">
- ''' Key already exists.;section
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Shadows Sub AddRange(ByVal keys As IniKey())
- For Each key As IniKey In keys
- Me.Add(key)
- Next key
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Adds the specified keys to the end of the <see cref="IniKeyCollection"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="keyNames">
- ''' The names of the keys to add.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="ArgumentException">
- ''' Section already exists.;section
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Shadows Sub AddRange(ByVal keyNames As String())
- For Each keyName As String In keyNames
- Me.Add(New IniKey(keyName, String.Empty))
- Next keyName
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Removes a <see cref="IniKey"/> from the <see cref="IniKeyCollection"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="key">
- ''' The key to remove.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="ArgumentException">
- ''' Key doesn't exists.;key
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Shadows Sub Remove(ByVal key As IniKey)
- Dim indexOf As Integer = Me.IndexOf(key)
- If (indexOf = -1) Then
- Throw New ArgumentException(message:="Key doesn't exists.", paramName:="key")
- Else
- MyBase.RemoveAt(indexOf)
- End If
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Removes a <see cref="IniKey"/> from the <see cref="IniKeyCollection"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="keyName">
- ''' The key to remove.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="ArgumentException">
- ''' Key doesn't exists.;keyName
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Shadows Sub Remove(ByVal keyName As String)
- Dim indexOf As Integer = Me.IndexOf(keyName)
- If (indexOf = -1) Then
- Throw New ArgumentException(message:="Key doesn't exists.", paramName:="keyName")
- Else
- MyBase.RemoveAt(indexOf)
- End If
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Determines whether the <see cref="IniKeyCollection"/> contains a <see cref="IniKey"/> that
- ''' matches the specified key name.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="keyName">
- ''' The key name.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see langword="True"/> if the <see cref="IniKeyCollection"/> contains the <see cref="IniKey"/>,
- ''' <see langword="False"/> otherwise.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overloads Function Contains(ByVal keyName As String) As Boolean
- Return (From key As IniKey In Me
- Where key.Name.Equals(keyName, StringComparison.OrdinalIgnoreCase)).Any
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Searches for an <see cref="IniKey"/> that matches the specified key name,
- ''' and returns the first occurrence within the entire <see cref="IniKeyCollection"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="keyName">
- ''' The key name.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IniKey"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overloads Function Find(ByVal keyName As String) As IniKey
- Return (From key As IniKey In Me
- Where key.Name.Equals(keyName, StringComparison.OrdinalIgnoreCase)).
- DefaultIfEmpty(Nothing).
- SingleOrDefault
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Searches for an <see cref="IniKey"/> that matches the specified key name and
- ''' returns the zero-based index of the first occurrence within the entire <see cref="IniKeyCollection"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="keyName">
- ''' The key name.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The zero-based index of the first occurrence of <see cref="IniKey"/> within the entire <see cref="IniKeyCollection"/>, if found;
- ''' otherwise, <c>–1</c>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overloads Function IndexOf(ByVal keyName As String) As Integer
- Dim index As Integer = 0
- Dim found As Boolean = False
- For Each key As IniKey In Me
- If key.Name.Equals(keyName, StringComparison.OrdinalIgnoreCase) Then
- found = True
- Exit For
- End If
- index += 1
- Next key
- If (found) Then
- Return index
- Else
- Return -1
- End If
- End Function
- #End Region
- End Class
- End Namespace
- #End Region
Add Comment
Please, Sign In to add comment