ARTICLE

System.Array Class using VB.NET: Part 2

Posted by Manish Tewatia Articles | Visual Basic Language August 10, 2010
In this article I will explain you about the System.Array Class using VB.NET.
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor