Click to See Complete Forum and Search --> : WTF? Removing an object from an array...


rghthnrblmrc
12-20-2006, 07:00 PM
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:

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.

russell
12-20-2006, 07:10 PM
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

rghthnrblmrc
12-20-2006, 07:41 PM
Well—they are declared, but i don't have the whole class there...

Here's a bigger chunk

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

russell
12-20-2006, 09:31 PM
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

russell
12-20-2006, 09:34 PM
here (http://www.webdeveloper.com/forum/showpost.php?p=684574&postcount=5) are some array functions u may find useful in this instance

rghthnrblmrc
12-21-2006, 09:43 AM
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.