Creating the ComputerList Class


Add a new class to the project and call it ComputerList. To keep this simple, use the code in Listing 10-1 for the class.

Listing 10-1: The ComputerList Class

start example
 Public Class ComputerList     Private mstrName As String     Private mstrProc As String     Private mdblSpeed As Double     Private mdblPrice As Double     Private mstrManuf As String     Public ReadOnly Property Proc() As String         Get             Return mstrProc         End Get     End Property     Public ReadOnly Property Speed() As Double         Get             Return mdblSpeed         End Get     End Property     Public ReadOnly Property Cname() As String         Get             Return mstrName         End Get     End Property     Public ReadOnly Property Price() As Double         Get             Return mdblPrice         End Get     End Property     Public ReadOnly Property Manufacturer() As String         Get             Return mstrManuf         End Get     End Property     Public Sub New(ByVal Name As String, ByVal Process As String, _     ByVal Sp As Double, ByVal Pr As Double, ByVal Man As String)         mstrName = Name         mstrProc = Process         mdblSpeed = Sp         mdblPrice = Pr         mstrManuf = Man     End Sub End Class 
end example

This simple class has five private variables and five public read-only variables. The variables all get set in the constructor. Next, you are going to tag three of the properties with your ListAttribute class so that only those three properties show up in the list. Add a List tag in front of the properties Cname, Process, and Speed so that each property looks like that in Listing 10-2.

Listing 10-2: Three Properties with the ListAttribute Applied

start example
 <List("Processor", 1)> Public ReadOnly Property Proc() As String      Get           Return mstrProc      End Get End Property <List("Speed", 2)> Public ReadOnly Property Speed() As Double      Get           Return mdblSpeed      End Get End Property <List("Computer Name", 0)> Public ReadOnly Property Cname() As String      Get           Return mstrName      End Get End Property 
end example

You will note that when you open the attribute tag (<) only the word List appears in the list of available attributes not the whole class name, ListAttribute. As mentioned previously, the word Attribute is dropped from the end of the attribute class. That is all you need to do to set up the ComputerList class. Now you need to create a collection class to hold a couple of values.

Create a class (in the same code module as the ComputerList class) called ComputerListMgr that inherits from the CollectionBase class. Use the code in Listing 10-3.

Listing 10-3: The ComputerListMgr Class

start example
 Public Class ComputerListMgr     Inherits System.Collections.CollectionBase     Public Sub Add(ByVal obj As ComputerList)         list.Add(obj)     End Sub     Public Sub Remove(ByVal Index As Integer)         list.RemoveAt(Index)     End Sub     Public Function Item(ByVal Index As Integer) As ComputerList         Return CType(list.Item(Index), ComputerList)     End Function End Class 
end example




Building Client/Server Applications with VB. NET(c) An Example-Driven Approach
Building Client/Server Applications Under VB .NET: An Example-Driven Approach
ISBN: 1590590708
EAN: 2147483647
Year: 2005
Pages: 148
Authors: Jeff Levinson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net