Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with Inheritance in Visual Basic.Net (Part 2)

Author: DevASP
Download Source Code : 446_Building a Custom Collection Class.zip

This is the second part of Building a collection class in this part I will discuss the second class of sample code.Article demonstrates the usage of the System.Collections.CollectionBase class.

This sample uses two classes: Customer and Customers. Customers inherits from System.Collections.CollectionBase in order to create a type safe collection to manage multiple customer instances. The CollectionBase class exposes an Inner List instance which is used to store Customer instance references.

 

The Customers class exposes overloaded versions of Add, Item, and Remove to facilitate management of the Customer instances. Unlike earlier versions of Visual Basic where optional parameters might have been used, overloaded members provide a more type safe way of accessing and managing a collection of internal types.

 

For the better Understanding Follow these Steps.

 

1-Make Two class Libraries

Class 2nd actually Used for two things The first thing to do when building a CollectionBase class is to inherit from System.Collections. CollectionBase. After you inherit the CollectionBase class you can access an intrinsic object called InnerList that presents your collection. InnerList is of type ArrayList.

 

Public Class Customers

      Inherits System.Collections.CollectionBase

 

public Overloads Function Add(ByVal Value As Customer) As Customer

 

            Me.InnerList.Add(Value)

            Return Value

 

End Function

 

 

Public Overloads Function Add(ByVal FirstName As String, ByVal LastName As String, ByVal AccountNum As String) As Customer

 

            Dim cust As New Customer()

            cust.FirstName = FirstName

            cust.LastName = LastName

            cust.AccountNumber = AccountNum

            Me.InnerList.Add(cust)

            Return cust

 

End Function

 

When you use the InnerList.Add method, you must pass it an object to add to the collection. In this case we've created a new Customer object based on passed parameters, and now we're adding it to the InnerList.

 

 

Public Overloads Function Item(ByVal Index As Integer) As Customer

 

            Return CType(Me.InnerList.Item(Index), Customer)

 

End Function

To retrieve an item from the InnerList, pass the index of that item to the .Item property.

 

Public Overloads Function Item(ByVal cust As Customer) As Customer

 

            Dim myIndex As Integer

            myIndex = Me.InnerList.IndexOf(cust)

            Return CType(Me.InnerList.Item(myIndex), Customer)

 

End Function

 

'To retrieve an item from the InnerList, pass the index of that item to the .Item property.

 

Public Overloads Sub Remove(ByVal cust As Customer)

 

            Me.InnerList.Remove(cust)

 

End Sub

To remove an item from the collection, you must pass in a reference to that item (in this case, the Customer object we want to remove). However, it is often more convenient to create a Remove method that allows the calling code to pass in only the index of the item instead of an object reference. So we've overloaded the Remove method to use either one.

 

Public Overloads Sub Remove(ByVal Index As Integer)

 

      Dim cust As Customer

      cust = CType(Me.InnerList.Item(Index), Customer)

      If Not cust Is Nothing Then

            Me.InnerList.Remove(cust)

      End If

 

End Sub

 

This is the second Remove method. Instead of passing in an object reference, this Remove expects an index. The calling code can decide which one to call. If the calling code passes an index, you can simply look up that item by using the InnerList.Item method, then remove the item. Now Enter these Lines of Code in Show Detail’s Label Button ‘s Click Event. From here, we COULD simply display the information by using the listCustomer object instead of looking up the primary copy in the Customers collection.However, we will be looking up the item in the Customers collection since we went through the trouble of creating the collection to begin with. Also to show how it is done.

Private Sub cmdItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdItem.Click

 

Dim listCustomer As Customer

      Dim cust As Customer

      If lstItems.SelectedIndex > -1 Then

            listCustomer = lstItems.SelectedItem

      Try

      cust = myCustomers.Item(listCustomer)

MessageBox.Show(cust.AccountNumber & " " & cust.FirstName & _

" " & cust.LastName, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)

      Catch exp As Exception

MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)

      End Try

      End If

 

End Sub

 

Now Enter these Lines of Code in Remove Button ‘s Click Event.From here, we COULD simply remove the collection item by using the listCustomer object instead of looking up the primary copy in the Customers collection.However, we will be looking up the item in the Customers collection since we went through the trouble of creating the collection to begin with. Also to show how it is done.

Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click

 

      Dim listCustomer As Customer

      Dim cust As Customer

If lstItems.SelectedIndex > -1 Then

            listCustomer = lstItems.SelectedItem

            Try

                  cust = myCustomers.Item(listCustomer)

                  myCustomers.Remove(cust)

                  lstItems.Items.Remove(listCustomer)

 

            Catch exp As Exception

MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

      End If

 

End Sub

Instructions for the source Code.

Enter some data for the first name, last name, and account number in the three text boxes on the main form and click the Add button. This will add a Customer object to the Customers collection with the data you specified. The account number for each customer added to the collection will appear in the listbox. After there is at least one Customer in the collection, you can select it in the listbox. Then you can remove it from the collection or show the details of a particular Customer in the collection. You can also click "Reload from collection" to clear the listbox and reload each item in the collection into the listbox.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Inheritence in Visual Basic.Net(Part 1)

Disclaimer - Privacy
© 2002-2017 DevASP.net