Presentation is loading. Please wait.

Presentation is loading. Please wait.

VB List(Of type) A List can be thought of as an array that automatically adjusts its size as elements are added and removed A List may hold only objects.

Similar presentations


Presentation on theme: "VB List(Of type) A List can be thought of as an array that automatically adjusts its size as elements are added and removed A List may hold only objects."— Presentation transcript:

1 VB List(Of type) A List can be thought of as an array that automatically adjusts its size as elements are added and removed A List may hold only objects of type Because it automatically varies in size, a List is more flexible than an array Elements can be accessed by index just as with an array: list(i)

2 List Methods Add(obj) : Adds obj to the list Remove(obj) : Removes obj from the list RemoveAt(i) : Removes the object from position i of the list Clear() : Clears the list Contains(obj) : Determines whether obj is in the list or not

3 List Methods (Cont'd) Insert(i, obj) : Inserts obj at position i of the list Reverse() : Reverses the order of the list IndexOf(obj) : Returns the index of obj ToArray() : Returns the elements of the list in an array of type

4 List Properties Count : Number of elements in the list (read-only) Item(i) : Gets or sets the element at position i in the list –Equivalent to list(i) : list(i) = value value = list(i)

5 List Examples Dim SomeList as List(Of SomeData) For Each s As SomeData In SomeList Console.WriteLine(s.ToString) Next For i As Integer = 0 To SomeList.Count Console.Writeline(SomeList(i)) Next

6 Lists as Data Sources Lists of Class can be connected to a ListBox control as a data source You must identify: –the Class string property to display in the ListBox –the Class value to return when a ListBox item is selected –the List to act as a data source

7 Lists as Data Sources (Cont'd) To bind a List to a ListBox: ListBox.DisplayMember = Class.property ListBox.ValueMember = Class.property ListBox.DataSource = List To extract values from the ListBox: index = ListBox.SelectedIndex value = ListBox.SelectedValue ListItem = ListBox.SelectedItem

8 Lists as Data Sources (Cont'd) The ListBox must be updated when the List is updated: ListBox.DataSource = Nothing ListBox.DataSource = List The display and value members do not have to be updated This seems kludgy - but it works

9 ListBox Data Source Example The Province class has two properties: Code (ex: "ON") and Name (ex: "Ontario") Define and fill a List of Province: Dim Provinces = List(Of Province) Bind the list to a ListBox lbProv : lbProv.DisplayMember = "Code" lbProv.ValueMember = "Name" lbProv.DataSource = Provinces

10 ListBox Data Source Example An element of the province list can be accessed by index, as an item (Province object), or just its name can be accessed

11 ListBox Data Source Example Set a temporary variable: Dim p as Province Index access: p = Provinces(lb.SelectedIndex ) Item access: p = CType(lb.SelectedItem, Province) Value (Name) access: n As String = lb.SelectedValue

12 List Inheritance User-defined classes can inherit from List Such a class has all the capabilities of List plus whatever extra methods and properties the user may define Define: Public Class SomeList Inherits List(Of SomeClass)

13 List Inheritance (Cont'd) SomeList now has access to all List methods and properties

14 List Inheritance Example Imports System.IO Public Class SubjectsList Inherits List(Of Subject) Public Sub New(ByVal fileName As String) Dim fileHandle As StreamReader = File.OpenText(fileName) Do While fileHandle.Peek <> -1 Dim s As Subject = New Subject(fileHandle) Me.Add(s) Loop fileHandle.Close() End Sub End Class

15 List Inheritance Example Sample uses of a Subjects class: Dim Subjects as SubjectList = New SubjectList( fileName ) … Subjects.Add( New Subject( code, desc ) ) … tbCode.Text = Subjects(i).Code … Subjects.RemoveAt(i)


Download ppt "VB List(Of type) A List can be thought of as an array that automatically adjusts its size as elements are added and removed A List may hold only objects."

Similar presentations


Ads by Google