How to create an indexer property in Visual Basic .NET or in Visual Basic 2005

For a Microsoft Visual Basic 6.0 version of this article, see
286755 .

Summary

This article describes how to create an Indexer property in Visual Basic .NET or in Visual Basic 2005 by using the default property attribute.

NOTE: A Microsoft C# indexer appears to a Visual Basic programmer as a default Item property, and a Visual Basic default property appears to a Microsoft C# programmer as an indexer. Indexers permit instances of a class to be indexed in the same way as arrays.

Create a Class Library Project

This section describes how to create a Visual Basic .NET or Visual Basic 2005 class library project that implements an Indexer property.
  1. Create a new Visual Basic .NET or Visual Basic 2005 class library project. Name the project VBIndexer.
  2. In Solution Explorer, select Class1.vb, and then right-click it. Select Rename on the shortcut menu, and then name it
    FileByteArray.vb.
  3. Delete all lines of code from the FileByteArray class module. Paste the following lines of code into the class module:
    Imports System
    Imports System.IO

    Public Class FileByteArray
    Private MyStream As Stream

    Public Sub New(ByVal FileName As String)
    MyStream = New FileStream(FileName, FileMode.Open)
    End Sub

    Public Sub Close()
    MyStream.Close()
    MyStream = Nothing
    End Sub

    Default Public Property Item(ByVal index As Long) As Byte
    Get
    Dim buffer(1) As Byte
    MyStream.Seek(index, SeekOrigin.Begin)
    MyStream.Read(buffer, 0, 1)
    Return buffer(0)

    End Get
    Set(ByVal Value As Byte)

    Dim buffer(1) As Byte
    buffer(0) = Value
    MyStream.Seek(index, SeekOrigin.Begin)
    MyStream.Write(buffer, 0, 1)
    End Set
    End Property

    Public ReadOnly Property Length() As Long
    Get
    Return MyStream.Seek(0, SeekOrigin.End)
    End Get
    End Property

    End Class
  4. On the Build menu, click Build VBIndexer.

Create a Sample Text File

This section describes how to create a sample text file that you can use as an input file for testing the FileByteArray class.
  1. Click Start and then click Run. In the Open box, type notepad to open Notepad.
  2. Select the following text, right-click it, and then click Copy. In Notepad, click Paste on the Edit menu to paste the text:

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps
    over the lazy dog. The quick brown fox jumps over the lazy dog. The
    quick brown fox jumps over the lazy dog. The quick brown fox jumps over
    the lazy dog.
  3. Save this file as Test.txt in the root folder of drive C, and then close the file.

Create a Test Application

This section describes how to create a sample Visual Basic .NET or Visual Basic 2005 project to test the FileByteArray class.
  1. Open Visual Studio .NET or Visual Studio 2005. On the File menu, click New, and then click Project.
  2. Under Project Types, click Visual Basic Projects, and under Templates, click Windows Application.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  3. Name the project TestIndexer. Select
    Add to Solution, and then click OK. Form1 is added to the project by default.Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

    For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

  4. Place a Command button on Form1, and then change its Text property to Reverse File.
  5. In Solution Explorer, right-click TestIndexer, and then select Set as Startup Project on the shortcut menu.
  6. Right-click TestIndexer again, and then select Add Reference on the shortcut menu. In the Add Reference dialog box, select the Projects tab. Double-click the VBIndexer project, and then click OK.
  7. In the Designer window, right-click Form1 and then select View Code.
  8. At the top of the Form1 code window, add the following Imports statement:
    Imports VBIndexer
  9. In the Designer window for Form1, double-click Button1. In the code window, replace the Button1_Click event procedure with the following lines of code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim FileObj As FileByteArray = New FileByteArray("C:\TEST.TXT")
    Dim len, i As Long
    len = FileObj.Length
    For i = 0 To len / 2
    Dim t As Byte
    t = FileObj(i)
    FileObj(i) = FileObj(len - i - 1)
    FileObj(len - i - 1) = t
    Next
    MessageBox.Show(FileObj.Length.ToString)

    FileObj.Close()
    End Sub
  10. Build the TestIndexer project. On the Debug menu, click Start to run the application.
  11. Click the Command button to reverse the file bytes. The message box displays the number of bytes that are reversed.
  12. In Microsoft Windows Explorer, find the Test.txt file on drive C, and then double-click it to open the file. Note that the contents of the file are reversed.

Code Discussion

  • In this example, you access an indexer by using the () operator on the base class object. However, you also can access an indexer by using the Item property, for example, FileObj.Item(i).
  • In the preceding example, Indexer (Item property in this case) is of type byte and takes a single index of type long (64-bit integer). The Get accessor defines the code to read a byte from the file, and the Set accessor defines the code to write a byte to the file. Inside the Set accessor, the predefined parameter Value has the value that is being assigned to the virtual array element.
  • To set the default attribute of a property in Visual Basic .NET or Visual Basic 2005, at least one parameter is required.
  • Because indexers are a powerful feature, it is important to use them only when the array-like abstraction makes sense. Always carefully consider whether it is just as clear to use regular methods.
  • For a collection class, an Item property is a good candidate for an Indexer. In collections, the syntax Collection.Item(0) and Collection(0) is frequently used.

REFERENCES

For additional information about default properties, see the "Default Properties for Your Components" topic in the Visual Studio .NET Online Help.

For additional information about the Collection class, see the "Creating Your Own Collection Class Walkthroughs" topic in the Visual Studio .NET or Visual Studio 2005 Online Help.

Propriedades

ID do Artigo: 311323 - Última Revisão: 11 de mai de 2007 - Revisão: 1

Comentários