Microsoft KB Archive/311323

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

Article ID: 311323

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q311323





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

IN THIS TASK
SUMMARY
 * Create a Class Library Project
 * Create a Sample Text File
 * Create a Test Application
 * Code Discussion

REFERENCES



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.

back to the top

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.

 Create a new Visual Basic .NET or Visual Basic 2005 class library project. Name the project VBIndexer . In Solution Explorer, select Class1.vb, and then right-click it. Select Rename on the shortcut menu, and then name it FileByteArray.vb .  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  On the Build menu, click Build VBIndexer.

back to the top

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.  Click Start and then click Run. In the Open box, type notepad to open Notepad.</li>  Select the following text, right-click it, and then click Copy. In Notepad, click Paste on the Edit menu to paste the text: <pre class="fixed_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. </li> Save this file as Test.txt in the root folder of drive C, and then close the file.</li></ol>

back to the top

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.  Open Visual Studio .NET or Visual Studio 2005. On the File menu, click New, and then click Project.</li> Under Project Types, click Visual Basic Projects, and under Templates, click Windows Application.

Note In Visual Studio 2005, click Visual Basic under Project Types.</li> 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:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

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

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

</li> Place a Command button on Form1, and then change its Text property to Reverse File.</li> In Solution Explorer, right-click TestIndexer, and then select Set as Startup Project on the shortcut menu.</li> 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.</li> In the Designer window, right-click Form1 and then select View Code.</li>  At the top of the Form1 code window, add the following Imports statement: Imports VBIndexer </li>  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(&quot;C:\TEST.TXT&quot;) 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 </li> Build the TestIndexer project. On the Debug menu, click Start to run the application.</li> Click the Command button to reverse the file bytes. The message box displays the number of bytes that are reversed.</li> 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.</li></ol>

back to the top

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.

back to the top