Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Object Oriented Programming

Vorig Onderwerp

Procedures and Functions

|

New in Visual Basic 2008 - 9.0

Volgend Onderwerp

Collections - Introduction

Vorig Onderwerp

Containment

|

Inheritance - Inherits

Volgend Onderwerp
Indexer Property

Indexer Property

Default Property

Default Property

Exercise

Exercise



Often applications need to work with collections of objects. Just like we've created arrays of 'Integer' and 'String' elements, we can create arrays with elements of our own custom created types ( classes ).

Following is an example of an array with elements of type Person.


Class Person
    Private m_Name As String
    Public Property Name() As String
        Get
            Name = m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
End Class
Module Client1
    Dim persons As Person()
    Dim personsCount As Integer
    Sub Main()
        Dim person1 As Person = New Person
        person1.Name = "John"
        '
        Dim person2 As Person = New Person
        person2.Name = "Jane"
        '
        Console.WriteLine(personsCount)
        '
        AddPerson(person1)
        AddPerson(person2)
        '
        Console.WriteLine(personsCount)
        '
        Console.WriteLine(persons(1).Name)
        '
        Console.ReadLine()
    End Sub
    Sub AddPerson(ByVal item As Person)
        ReDim Preserve persons(personsCount)
        persons(personsCount) = item
        personsCount += 1
    End Sub
End Module
Download Broncode

Output :

 0
 2
 Jane

The client is responsible for defining the collectionlogic. The client has define how the elements are stored, and in what order. The client has to define what happens when an element is added, ... .

We could also isolate this logic into a collectiontype. Client then only have to create an object of this type, to create a collection.


Class Persons
    Private m_Items As Person()
    Default Public ReadOnly Property Item(ByVal index As Integer) As Person
        Get                                                                ' (1)
            Item = m_Items(index)
        End Get
    End Property
    Private m_Count As Integer
    Public ReadOnly Property Count() As Integer
        Get
            Count = m_Count
        End Get
    End Property
    Public Sub Add(ByVal item As Person)
        ReDim Preserve m_Items(Count)
        m_Items(Count) = item
        m_Count += 1
    End Sub
End Class
Module Client2
    Sub Main()
        Dim person1 As Person = New Person
        person1.Name = "John"
        '
        Dim person2 As Person = New Person
        person2.Name = "Jane"
        '
        Dim persons1 As Persons = New Persons
        '
        Console.WriteLine(persons1.Count)
        '
        persons1.Add(person1)
        persons1.Add(person2)
        '
        Console.WriteLine(persons1.Count)
        '
        Console.WriteLine(persons1(1).Name)                                ' (2)
        Console.WriteLine(persons1.Item(1).Name)                           ' (3)
        '
        Console.ReadLine()
    End Sub
End Module
Module Client3
    Sub Main()
        Dim person1 As Person = New Person
        person1.Name = "John"
        '
        Dim persons1 As Persons = New Persons
        persons1.Add(person1)
    End Sub
End Module
Download Broncode

Output :

 0
 2
 Jane
 Jane

Different clients ( 'Client2' and 'Client3' ) can now create a collectionobject, and don't need to define all logic about managing such a collection.

Notice how the identifier PersonS ( plural ) makes it clear that an instance of this type represents multiple persons.


Indexer Property


Line (1) indicates the getter of an "indexer". This is a typical member for a collection of which the elements need to be referred to by an index.
This indexer delivers a Person object, the type specifier in the signature of this indexer refers to the Person type.

As you can see properties can also have arguments. They could have multiple arguments, optional arguments, work by value or by reference with the
argument value, ... .

In the above abstraction the indexer is made readonly, this doesn't always have to be the case, maybe a writeable property is more suitable for your concept.


Klik hier om terug naar boven te gaan.  Up


Default Property


The keyword 'Default', here used for the indexer, defines that a client doesn't need to explicitly refer to the identifier of this property ( 'Item' ) (2) (3). Immediately following the identifier of the collectionobject you can place the argument list, here referring to the index of the element to be addressed.

Default can only be used on properties with arguments. Calls ( without explicitly using the identifier ) to the to default property without arguments would look exactly the same as the object expression on which it is called. To avoid this ambiguity, one can only make properties with arguments default.

The above example is a very basic abstraction for a collection. Only limited collectionlogic is includes. Often collections are more complex.
Its advisable to make the collection symmetrical, if you have an add-item functionality, youll probably need a remove-item functionality, ... .

Further on we discuss the predefined collection structures of the .NET FCL ( Framework Class Library ), and how we can or need to adapt these structures.


Klik hier om terug naar boven te gaan.  Up


Exercise


Task :

Design a class Rectangle. Rectangle objects have a height and width property ( readable and writeable ). Make it possible to query the rectangle objects for their area.

Design a collectiontype to manage a collection of rectangles. Elements of this collection can be addressed by their index.
Make it possible to add rectangle objects, and to remove rectangle objects at a specific index.
The collection can be queried for the total area, the sum of all areas of all elements.


Solution :


Class Rectangle
    Private m_Height As Integer
    Public Property Height() As Integer
        Get
            Height = m_Height
        End Get
        Set(ByVal value As Integer)
            m_Height = value
        End Set
    End Property
    Private m_Width As Integer
    Public Property Width() As Integer
        Get
            Width = m_Width
        End Get
        Set(ByVal value As Integer)
            m_Width = value
        End Set
    End Property
    Public Function GetArea() As Integer
        GetArea = Height * Width
    End Function
End Class
Class Rectangles
    Private m_Items As Rectangle()
    Default Public ReadOnly Property Item(ByVal index As Integer) As Rectangle
        Get
            Item = m_Items(index)
        End Get
    End Property
    Private m_Count As Integer
    Public ReadOnly Property Count() As Integer
        Get
            Count = m_Count
        End Get
    End Property
    Public Sub Add(ByVal rectangle As Rectangle)
        ReDim Preserve m_Items(Count)
        m_Items(Count) = rectangle
        m_Count += 1
    End Sub
    Public Sub RemoveAt(ByVal index As Integer)
        For itemIndex As Integer = index To Count - 2
            m_Items(itemIndex) = m_Items(itemIndex + 1)
        Next
        ReDim Preserve m_Items(Count - 2)
        m_Count -= 1
    End Sub
    Public Function GetTotalArea() As Integer
        For Each item As Rectangle In m_Items
            GetTotalArea += item.GetArea()
        Next
    End Function
End Class
Module Exercise1Solution
    Sub Main()
        Dim rectangle1 As Rectangle = New Rectangle
        rectangle1.Height = 1
        rectangle1.Width = 2
        Console.WriteLine(rectangle1.GetArea())
        '
        Dim rectangle2 As Rectangle = New Rectangle
        rectangle2.Height = 3
        rectangle2.Width = 4
        Console.WriteLine(rectangle2.GetArea())
        '
        Dim rectangles1 As Rectangles = New Rectangles
        rectangles1.Add(rectangle1)
        rectangles1.Add(rectangle2)
        '
        For index As Integer = 0 To rectangles1.Count - 1
            Console.Write(rectangles1(index).GetArea() & " ")
        Next
        Console.WriteLine()
        Console.WriteLine(rectangles1.GetTotalArea())
        '
        rectangles1.RemoveAt(1)
        '
        For index As Integer = 0 To rectangles1.Count - 1
            Console.Write(rectangles1(index).GetArea() & " ")
        Next
        Console.WriteLine()
        Console.WriteLine(rectangles1.GetTotalArea())
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 2
 12
 2 12
 14
 2
 2

Updated On : 2008-09-25

Download Broncode

Published On : 2008-11-06

Collections - Introduction

Vorig Onderwerp

Containment

|

Inheritance - Inherits

Volgend Onderwerp

Object Oriented Programming

Vorig Onderwerp

Procedures and Functions

|

New in Visual Basic 2008 - 9.0

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.