www.webdeveloper.com
Results 1 to 6 of 6

Thread: WTF? Removing an object from an array...

  1. #1
    Join Date
    Dec 2006
    Posts
    25

    WTF? Removing an object from an array...

    I've tried to write a function to remove an object from an array:

    members() belongs to the class to which this is a method:

    Code:
    Public Function RemoveCartMember(ByVal id)
    		Dim i, j, newcart(), itm
    		j = 0
    		For i=0 To Length-1
    			
    			If Not i = id Then
    				Set itm = members(i)
    				j = j + 1
    				Redim Preserve newcart(j+1)
    				
    				Set newcart(j) = itm
    			End If
    		Next
    		Set itm = Nothing
    		
                   members = newcart
    		
    	End Function
    I get a type mismatch... I don't understand why i can't set members to newcart. Thanks.

  2. #2
    Join Date
    Feb 2003
    Posts
    2,745
    too many undeclared variables to say for sure (might help if u post entire class) but it looks like u want to Set members = newcart, instead of just memebers = newcart

  3. #3
    Join Date
    Dec 2006
    Posts
    25
    Well—they are declared, but i don't have the whole class there...

    Here's a bigger chunk

    Code:
    Class ShoppingCart
    	Private itemCount
    	Private members()
    
    	Private Sub Class_Initialize
    		itemCount = 0
    	End Sub
    	
    	Private Sub Class_Terminate
    		Dim i
    		For i=0 To itemCount
    			Set members(i) = Nothing
    		Next
    	End Sub
    	
    	Private Function AddCartMember(ByRef mArray, ByRef mObj)
    		Dim itemIndex
    		
    		itemIndex = itemCount + 1
    		
    		Redim Preserve mArray(itemIndex)
    		Set mArray(itemCount) = mObj
    		
    		AddCartMember = itemIndex
    	End Function
    	
    	Public Function RemoveCartMember(ByVal id)
    		Dim i, j, newcart(), itm
    		j = 0
    		For i=0 To Length-1
    			Response.Write(i)
    			If Not i = id Then
    				Set itm = members(i)
    				j = j + 1
    				Redim Preserve newcart(j+1)
    				
    				Set newcart(j) = itm
    			End If
    		Next
    		Set itm = Nothing
    		
    		members = newcart
    		
    	End Function
    	
    	Public Sub PopulateCart(ByVal localcart, ByVal sessionShippingTotal)
    		Dim cartMember, cartMemberCount, isMembers, i
    		
    		shippingTotal = sessionShippingTotal
    		
    		cartMemberCount = UBound(localcart,2)
    		isMembers = cartMemberCount > 0
    		For i=0 To cartMemberCount
    			Set cartMember = New ShoppingCartMember
    			If localcart(CARTDESC, i) <> "" Then
    				cartMember.PopulateCartMember localcart(CARTMANU, i), localcart(CARTMODELNUM, i), localcart(CARTDESC, i), localcart(CARTPRICE, i), localcart(CARTQUANTITY, i), localcart(CARTSHIPPINGCOMB, i), localcart(CARTSHIPPING, i)
    			 	itemCount = AddCartMember(members, cartMember)
    			End If
    		Next
    	End Sub
    	
    	Public Property Get Length
    		Length = itemCount
    	End Property
    	
    	Public Function GetCartMember(ByVal index)
    		If UBound(members) < index Or Not isObject(members(index)) Then
    			Set GetCartMember = Nothing
    		Else
    			Set GetCartMember = members(index)
    		End If
    	End Function
    	
    	
    End Class
    Last edited by rghthnrblmrc; 12-21-2006 at 09:45 AM.

  4. #4
    Join Date
    Feb 2003
    Posts
    2,745
    i figured they were, but in the code u hadnt posted earlier. i see that the RemoveCartMember() method has changed between posts. is your problem resolved? also, am i missing something or is Length not declared?

    For i=0 To Length-1

  5. #5
    Join Date
    Feb 2003
    Posts
    2,745
    here are some array functions u may find useful in this instance

  6. #6
    Join Date
    Dec 2006
    Posts
    25
    Thank you for these functions. I appreciate it. They'll need to be adapted for this use, since I am dealing with objects and they require "Set" I would love to have these standard array functions available in VBScript. Without them, everything is like pulling teeth.

    That is still not the whole class. I have removed any of the methods containing the actual algorithms and have left only the structure: the basic input-output. Length is defined in its own block: Public Property Get Length. Right now that just returns itemCount, but obviously I will need some extra logic, like UBound(members) or something.

    As far as the discrepancy between the two functions, yes, but that was accidental. The one I have tested is the first. It doesn't work. thanks.

    I'll look into adapting those array functions to work with objects too.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles