System.Array Class using VB.NET: Part 2

In this article I will explain you about the System.Array Class using VB.NET.
  • 2693

As in my previous article you was see the example of Looping Through Array Class Objects. Now in this article we talk about the .NET Framework which provides IEnumerable and IEnumerator interfaces to implement and provide a collection like behavior to user-defined classes. These interfaces are implemented through inner classes. An inner or nested-type class is enclosed inside another class. The given bellow example shows the implementation of the enumerator.

Example of Creating a custom Enumerator

    Imports System
    Imports System.Collections

    Class ItemCollection
        Implements IEnumerable
        ' COLLECTION
    End Class

    Class
ItemIterator
        Implements IEnumerator
        ' ITERATOR
        Private Class ItemCollection
            ' class ItemCollection is an Inner Class or Nested Type in class ItemIterator
        End Class
        'IEnumerator and IEnumerable interfaces are defined in System.Collections
        'namespace as:
        Public Interface IEnumerable
            Function GetEnumerator() As IEnumerator
            'returns an enumerator
        End Interface

        Public
Interface IEnumerator
            Function MoveNext() As Boolean
            'After an enumerator is created or after a Reset,
            'an enumerator is positioned before the first element
            'of the collection, and the first call to MoveNext
            'moves the enumerator over the first element of the
            'collection.
            ' And next calls move the cursor one further till the end.
            ReadOnly Property Current() As Object

            ' Returns the current object from the collection.
            ' You should throw an InvalidOperationException exception
            ' if index pointing to wrong position.

            Sub Reset()
            ' Resets enumerator to just before the first element of the collection.
            ' Resets pointer to -1.
        End Interface
    End
Class

In VB.NET all array elements are initialized to their default values. For reference-type variables, the default value is null. You need to instantiate the reference element before you can access any member property; otherwise you receive an error. As shown in below example, you can also use the Array.Initialize member function to initialize every element of a value-type array by calling the default constructor of the value type.

Example of Array Initialization

    Public Class DisplayPreferences
        Private m_PropertyID As String
        Public
Sub New()
            m_PropertyID =
Nothing
        End
Sub

        Public
Property PropertyID() As String
            Get
                Return
m_PropertyID
            End Get
            Set
(ByVal value As String)
                m_PropertyID = value
            End Set
        End
Property
    End
Class

    Public
Class MySampleClass
        Public Shared Sub Main()
            Dim oPrefs As DisplayPreferences() = New DisplayPreferences(9) {}
            ' you have to initiliaze array items before use...
            For i As Integer = 0 To oPrefs.Length - 1
                oPrefs(i) =
New DisplayPreferences()
            Next
            oPrefs(0).PropertyID = "ID007"
            ' or if the array is of value-type, not necessary but...
            Dim intarr As Integer() = New Integer(9) {}
            intarr.Initialize()
            intarr(0) = 7
        End Sub
    End
Class

Conclusion

Hope this article would have helped you in understanding the System.Array Class using VB.NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.