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 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
- Delete all lines of code from the FileByteArray class module. Paste the following lines of code into the class module:
Public Class FileByteArray
Private MyStream As Stream
Public Sub New(ByVal FileName As String)
MyStream = New FileStream(FileName, FileMode.Open)
Public Sub Close()
MyStream = Nothing
Default Public Property Item(ByVal index As Long) As Byte
Dim buffer(1) As Byte
MyStream.Read(buffer, 0, 1)
Set(ByVal Value As Byte)
Dim buffer(1) As Byte
buffer(0) = Value
MyStream.Write(buffer, 0, 1)
Public ReadOnly Property Length() As Long
Return MyStream.Seek(0, SeekOrigin.End)
- On the Build menu, click Build VBIndexer.
- Click Start and then click Run. In the Open box, type notepad to open Notepad.
- 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.
- Save this file as Test.txt in the root folder of drive C, and then close the file.
- Open Visual Studio .NET or Visual Studio 2005. On the File menu, click New, and then click Project.
- Under Project Types, click Visual Basic Projects, and under Templates, click Windows Application.
Note In Visual Studio 2005, click Visual Basic under Project Types.
- 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:
- Place a Command button on Form1, and then change its Text property to Reverse File.
- In Solution Explorer, right-click TestIndexer, and then select Set as Startup Project on the shortcut menu.
- 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.
- In the Designer window, right-click Form1 and then select View Code.
- At the top of the Form1 code window, add the following Imports statement:
- 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
- Build the TestIndexer project. On the Debug menu, click Start to run the application.
- Click the Command button to reverse the file bytes. The message box displays the number of bytes that are reversed.
- 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.
- 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.
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.
ID do Artigo: 311323 - Última Revisão: 11 de mai de 2007 - Revisão: 1