Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Collection Class module
|
<H2>Introduction</H2> Two Files in this article. coll.asp is the collection library, main.asp is the sample of using the collection class.
<H2>Sample Demo Page</H2> <% '------------------------------------------------------------------------------ 'This is a sample of how to use the collection Class '------------------------------------------------------------------------------ 'Warning: This sample is using the Functionality provided by VBScript 5.x to 'install VBScript 5.x install IE5 or visit www.microsoft.com/vbscript '------------------------------------------------------------------------------ %> <!-- #include file="coll.asp"--> <% '---------------------------------------------------- 'this is Our Object Class that we want to make 'a collection of '---------------------------------------------------- Class Customer Public Name 'i've only used one property to keep the class simple ' this class can have as many properties or method needed 'etc... End Class
Dim oCustomers,i 'create Our Collections class Set oCustomers = new Collection
'---------------------------------------------------- 'just add all the item that you want 'into the collection '---------------------------------------------------- 'change the add function to take as many parameters as you want. 'refer to the [TODO] section in the Add function implementation oCustomers.Add("Sarah") oCustomers.Add("Sam") oCustomers.Add("Dolly") oCustomers.Add("Shadi") oCustomers.Add("Becky") oCustomers.Add("Licky")
'Enumerate through the collection 'Our collection class has the Count Property For i=0 to oCustomers.Count-1 'to access an item in our collection use the Item 'function to return the object and VBScripts late binding's 'functionality to access it's Properties Response.Write oCustomers.Item(i).Name & "<br>" Next
'This is an example of removing an element 'from our Collection oCustomers.Remove(3) Response.Write "<hr>"
'Enumerate through the collection to show the New Content 'after the remove. For i=0 to oCustomers.Count-1 Response.Write oCustomers.Item(i).Name & "<br>" Next
'delete the instance of the Collection 'free up resources. Set oCustomers = Nothing
%>
<H2>Collection page Code</H2> <% '------------------------------------------------------------------------------ 'This is the collection include that implements the Collection Class. 'this file must be included into the page that wants to use the collection 'service. '------------------------------------------------------------------------------ 'Warning: This sample is using the Functionality provided by VBScript 5.x to 'install VBScript 5.x install IE5 or visit www.microsoft.com/vbscript '------------------------------------------------------------------------------
'----------------------------------------- ' Class: Node ' Description: This class is used internally by the collection class to hold the ' the list of all objects in the list. '----------------------------------------- 'Note: this class should NOT! be modified. '----------------------------------------- Class Node Public Node Public pNext End Class
'---------------------------------------------------------------------------------- ' Class: Collection ' Description: This is our Collection class. ' Create an object of this class and modify it's ' Add functon's TODO section to hold a collections ' of any user defined object. '---------------------------------------------------------------------------------- 'Note: Only the Add function should be modifid in this class. '---------------------------------------------------------------------------------- Class Collection 'this is the member of the link list 'usd by the class to add its objects to. Private m_list
'------------------------- 'Function: Display '------------------------- 'internally used to debug '------------------------- Private Function Display() Dim opal 'set our pointer to the start of the listy Set opal = m_list 'loop until the end Do While(IsObject(opal)) 'output the information in the Node; '[TODO]: Modify this to output anyinformation 'example response.write opal.Node.xxxxx 'where xxxxx is any property of your object. Response.Write opal.Node.Name & "<br>" 'make sure that the next node is an object if(Not IsObject(opal.pNext)) then exit Do End if 'move next Set opal = opal.pNext Loop End function
'---------------------------------------------------- 'This function adds to the collection 'change this function to add more parameters, that 'will set properties in your object '---------------------------------------------------- 'example Add(property1,property2,property3,etc.. 'where: property[1-3] are properties of your class '---------------------------------------------------- Public Function Add(szName) '[TODO] change this to a variable of your class 'example Dim oMyclass Dim oCustomer Dim oNode,opal
'is the list empty if Not IsObject(m_list) then 'the list is empty create an object 'of the user defined class '[TODO] change from Customer to any 'user defined class. Set oCustomer = new Customer
'[TODO] change this to assign arguments of interest. oCustomer.Name = szName
'create a new node Set m_list =new Node 'assign our new node to the list Set m_list.Node = oCustomer Else 'the list is not empty so just add to it Set opal = m_list 'loop until the last node. Do While(IsObject(opal)) if(Not IsObject(opal.pNext)) then exit Do End if Set opal = opal.pNext Loop
'we are at the end instatiate our class 'assign our arguments and add to the list '[TODO] modify to suit accordingly Set oCustomer =new Customer oCustomer.Name = szName Set oNode = new Node
'insert into the end of the list Set oNode.Node = oCustomer oNode.pNext = opal.pNext Set opal.pNext = oNode End if End Function
'---------------------------------------------------- 'This function if you are a C++ person is the destructor 'this is an event that gets called when the class terminates '---------------------------------------------------- Private Sub Collection_Terminate() 'free all my resources. Call Me.Free() End Sub
'---------------------------------------------------- 'Free '---------------------------------------------------- Private Function Free() Dim opal,opalNext Set opal = m_list 'loop through the list and free Nodes. Do While(IsObject(opal)) if(Not IsObject(opal.pNext)) then exit Do End if Set opalNext = opal.pNext Set opal = Nothing Set opal = opalNext Loop Set opal = Nothing End Function
'---------------------------------------------------- 'Count This property get you the Collection Count '---------------------------------------------------- Property Get Count() Dim opal,i Set opal = m_list 'loop through the list and Count it's Nodes. Do While(IsObject(opal)) i = i + 1 if(Not IsObject(opal.pNext)) then exit Do End if Set opal = opal.pNext Loop Count = i End Property
'---------------------------------------------------- 'InternalItem this function is used internally to return 'a reference to an items Node. this defers from Item 'because Item returns a reference to the Items object '---------------------------------------------------- Private Function InternalItem(index) Dim opal,i,obj Set opal = m_list i = 0 Do While(IsObject(opal)) if index = i then Set obj = opal exit Do else i = i + 1 End if
if(Not IsObject(opal.pNext)) then exit Do End if Set opal = opal.pNext Loop if Not IsObject(obj) then InternalItem = Null Else Set InternalItem = obj End if End Function
'---------------------------------------------------- 'Item this function return the object of the index. '---------------------------------------------------- Function Item(index) Dim opal,i,obj Set opal = m_list i = 0 Do While(IsObject(opal)) if index = i then Set obj = opal.Node exit Do else i = i + 1 End if
if(Not IsObject(opal.pNext)) then exit Do End if Set opal = opal.pNext Loop if Not IsObject(obj) then Set Item = Null Else Set Item = obj End if End Function
'---------------------------------------------------- 'Remove this function removes an item from the collection '---------------------------------------------------- Function Remove(index) Dim oPrev,opal,oNext if index = 0 then if IsObject(m_list.pNext) then Set oNext = m_list.pNext Set m_list = oNext else Set m_list = Nothing End if elseif index > 0 And index <= Me.Count() then 'we use InternalItem because we want a 'refernce to the Node not the object Set oPrev = InternalItem(index-1) Set opal = InternalItem(index) if IsObject(opal.pNext) then Set oPrev.pNext = opal.pNext Else oPrev.pNext = NULL End if Set opal = Nothing End if End Function
End Class %>
<H2>Summary</H2>
These demo file can be use as example how to use collection class module in vb.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|