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 Inheritence in Visual Basic.Net(Part 1)

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

This Article demonstrates the usage of the System.Collections. CollectionBase class for creating a strongly-typed collection.

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 first actually Used for displaying data in the list box

 

Public Class Customer

      Private custAccount As String

      Private custFirstName As String

      Private custLastName As String

#Region "Customer Properties"

Public ReadOnly Property DisplayData() As String

      Get

      Return Me.custAccount & ": " & Me.FirstName & " " & Me.LastName

      End Get

      End Property

public Property AccountNumber() As String

            Get

                  Return custAccount

            End Get

            Set(ByVal Value As String)

                  custAccount = Value

            End Set

End Property

 

Public Property FirstName() As String

            Get

                  Return custFirstName

            End Get

            Set(ByVal Value As String)

                  custFirstName = Value

            End Set

End Property

 

Public Property LastName() As String

      Get

            Return custLastName

      End Get

      Set(ByVal Value As String)

            custLastName = Value

      End Set

End Property

#End Region

End Class

 

Now Enter these Lines of Code in Form ‘s Load Event. That Code runs when adding an object to a listbox, you must set the listbox "DisplayMember" property BEFORE adding the object. This will allow the listbox to display whatever property of the added object you want.Remove the comment below to change the binded property lstItems.DisplayMember = "AccountNumber". Load Some Samples Customers.

 

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim cust As Customer

            Dim listNumber As Integer

 

            lstItems.DisplayMember = "DisplayData"

 

            cust = myCustomers.Add("Tom", "Slick", "1234567890")

            listNumber = lstItems.Items.Add(cust)

 

            cust = myCustomers.Add("Bob", "Dabs", "9812462424")

            listNumber = lstItems.Items.Add(cust)

 

            cust = myCustomers.Add("Mike", "Chevel", "5783574123")

            listNumber = lstItems.Items.Add(cust)

 

      End Sub

 

Then add the region below. has been added to some procedures since they are not the focus of the demo. Remove them if you wish to debug the procedures.

 

#Region " Standard Menu Code "

      Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

           

      End Sub

 

Private Sub mnuFileClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

      Me.Close()

      End Sub

#End Region

 

 

Now write that code behind the add Button. This function add the values in our first Class ‘s Object. And After that we assign empty strings to our text boxes.

 

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click

Dim cust As Customer

Dim listNumber As Integer

cust = myCustomers.Add(txtFirstName.Text, txtLastName.Text, txtAccount.Text)

      listNumber = lstItems.Items.Add(cust)

      lstItems.Refresh()

      txtFirstName.Text = String.Empty

      txtLastName.Text = String.Empty

      txtAccount.Text = String.Empty

End Sub

 

Second class I will Explain You in next article .Source code is given with this article. please carefully read the Instructions below before running the sample code.Helps full Comments are Also There.

 

 

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 :
 
   
<< Creating a Control Array in VB.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net