PC Review


Reply
Thread Tools Rate Thread

Cannot reference object in custom collection

 
 
Dave
Guest
Posts: n/a
 
      13th Dec 2011
This one has be stumped. I loop through a collection of form textboxes
and add the control name and value as two properties of a class
object, then add the object to a collection. At any point while
stepping through, I can debug.print the two properties and check that
the collection count has incremented by 1. All would seem well.
However, if I reference the collection item in the immediate window
while stepping, the reference always returns the properties for the
last added item. For example, when the collection count is 6 and I ask
for the count [?colvariable.count] I get 6 . If I ask for the name and
value for item 2 [?colVariable.item(2).ctlname & "..." &
colvariable.Item(2).qty]
I get txtQty6... [no value because there isn't one]

If I do this when the first item holding a quantity has been added, I
get txtQty2...5, which is fine. It seems that whenever I check for
colVariable.item(n), it will always return the last item added to the
collection even though I've specified a key and it is different each
time.
It's probably something simple (sigh)...

Class Code (explicit option not shown) in clsVariable
Private svctlName As String
Private varQty As Variant

Public Property Get ctlName() As String
ctlName = svctlName
End Property
Public Property Let ctlName(value As String)
svctlName = value
End Property
Public Property Get Qty() As Variant
Qty = varQty
End Property
Public Property Let Qty(value As Variant)
varQty = value
End Property
*****
declaration section of form module:
Dim myClass As New clsVariable
Dim colVariable As New Collection
****
form listbox click event:
some other dim's
Dim ctl As Control
more code, then the loop
For Each ctl In Forms!frmJoblist!subWorkDteDtl.Form.Controls
ifs
end ifs
ctlAdd ctl.Name, ctl.value 'call the function to add the item
Next

Public Function ctlAdd(svctlName As String, varQty As Variant)
Dim svKey As String
With myClass
.ctlName = svctlName
.Qty = varQty
svKey = svctlName
End With
colVariable.add Item:=myClass, Key:=svKey
If myClass.Qty <> "" Then Debug.Print myClass.ctlName & " " &
myClass.Qty
End Function

Anyone who can solve this will be my hero!
Thanks in advance.
 
Reply With Quote
 
 
 
 
Dave
Guest
Posts: n/a
 
      14th Dec 2011
On Dec 13, 12:38*pm, Dave <yullneverk...@mail.com> wrote:
> This one has be stumped. I loop through a collection of form textboxes
> and add the control name and value as two properties of a class
> object, then add the object to a collection. At any point while
> stepping through, I can debug.print the two properties and check that
> the collection count has incremented by 1. All would seem well.
> However, if I reference the collection item in the immediate window
> while stepping, the reference always returns the properties for the
> last added item. For example, when the collection count is 6 and I ask
> for the count [?colvariable.count] I get 6 . If I ask for the name and
> value for item 2 [?colVariable.item(2).ctlname & "..." &
> colvariable.Item(2).qty]
> I get txtQty6... [no value because there isn't one]
>
> If I do this when the first item holding a quantity has been added, I
> get txtQty2...5, which is fine. It seems that whenever I check for
> colVariable.item(n), it will always return the last item added to the
> collection even though I've specified a key and it is different each
> time.
> It's probably something simple (sigh)...
>
> Class Code (explicit option not shown) in clsVariable
> Private svctlName As String
> Private varQty As Variant
>
> Public Property Get ctlName() As String
> ctlName = svctlName
> End Property
> Public Property Let ctlName(value As String)
> svctlName = value
> End Property
> Public Property Get Qty() As Variant
> Qty = varQty
> End Property
> Public Property Let Qty(value As Variant)
> varQty = value
> End Property
> *****
> declaration section of form module:
> Dim myClass As New clsVariable
> Dim colVariable As New Collection
> ****
> form listbox click event:
> some other dim's
> Dim ctl As Control
> more code, then the loop
> For Each ctl In Forms!frmJoblist!subWorkDteDtl.Form.Controls
> * * ifs
> * * end ifs
> * * ctlAdd ctl.Name, ctl.value 'call the function to add the item
> Next
>
> Public Function ctlAdd(svctlName As String, varQty As Variant)
> Dim svKey As String
> With myClass
> * * .ctlName = svctlName
> * * .Qty = varQty
> * * svKey = svctlName
> End With
> colVariable.add Item:=myClass, Key:=svKey
> If myClass.Qty <> "" Then Debug.Print myClass.ctlName & " * " &
> myClass.Qty
> End Function
>
> Anyone who can solve this will be my hero!
> Thanks in advance.


Sorry, the hero job is taken. The object was created outside the loop.
I moved it and it works fine.
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:23 AM.