The ArrayList Class


Array Lists



The main problem of traditional arrays is that their size is fixed by the number you specify when declaring the array variable: you cannot add items beyond the specified dimension. Another limitation is that you cannot insert an item inside the list. To overcome this, you can create a linked list. Instead of working from scratch, the .NET Framework provides the ArrayList class. With the ArrayList class, you can add new items to a list, insert items inside a list, arrange items of a list, check the existence of an item in a list, remove an item from the list, inquire about the list, or destroy the list. These operations are possible through various properties and methods.

The ArrayList class is defined in the System.Collections namespace. To use it, first declare a pointer to ArrayList. Here is an example:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim lstNumbers As ArrayList = New ArrayList
End Sub

Practical Learning Practical Learning: Introducing the ArrayList Class

  1. If you have not done so, create the StoreItem library now
  2. Start a new Windows Application named DepartmentStore2
  3. To add a library to this project, in the Solution Explorer, right-click References and click Add Reference...
  4. In the Add Reference dialog box, click Browse...
  5. Locate the folder that contains the StoreItem library and display it in the Look In combo box
  6. Double-click its bin sub-folder to display it in the Look In combo box
  7. In the Select Component dialog box, select StoreItem.dll (it should be selected already) and click Open
  8. In the Add Reference dialog box, click OK
  9. Design the form as follows:
    Control Name Text Other Properties
    TabPage pgeNewItem New Item  
    Label   Item #:  
    TextBox txtItemNumber    
    Label   Description:  
    TextBox txtDescription    
    Label   Size:  
    TextBox txtSize