collection and object question

hi, I am new to object and collection.
I am testing the sample application as follows, but I got the results as two txtUserxxx instead of txtUserxxx and txtPwdyyy. could anyone explains why?

thank you

codes:

Option Explicit
Public sbMain As New clsLogon

Private Sub cmdLogon_Click()

    Dim c As New clsInput
    Dim i, j
    Dim control
    For Each control In Me.Controls
   
    If TypeOf control Is TextBox Then
   
    With c
        .Name = control.Name
        .Value = control.Text
    End With
    sbMain.Inputs.Add c
    End If
    Next
    Dim inp As New clsInput
    Debug.Print sbMain.Inputs.Count
    For Each inp In sbMain.Inputs
        Debug.Print inp.Name & inp.Value
    Next
Set sbMain.Inputs = Nothing
End Sub
chrislee8Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

caraf_gCommented:
The good old Dim As New trap....

You've only got one object variable c.

What your loop does is the following:

First iteration, it processes txtUserxxx
  c.Name = txtUserxxx.Name
  c.Value = txtUserxxx.Text

Now it adds c to the collection. So element 1 in the collection points at c

Second iteration, it processes txtPwdyyy
  c.Name = txtPwdyyy.Name
  c.Value = txtPwdyyy.Text
now it adds c to the collection. So element 2 in the collection points at c. But so does element 1! Nothing's changed.

What has happened here?

You've got an object. And an object variable c that points to that object. Every time you add c to the collection, you merely add another pointer to the object. No matter whether you change
c.Name
sbMain(1).Name
or sbMain(2).Name

Whichever one you change, you make a change to the object, and therefore, all object variables and collection items pointing to that object reflect that change.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
caraf_gCommented:
You can fix the problem as follows

Option Explicit
Public sbMain As New clsLogon

Private Sub cmdLogon_Click()

    Dim c As clsInput
    Dim i, j
    Dim control
    For Each control In Me.Controls
     
    If TypeOf control Is TextBox Then
   
    Set c = New clsInput
    With c
        .Name = control.Name
        .Value = control.Text
    End With
    sbMain.Inputs.Add c
    End If
    Next
    Dim inp As New clsInput
    Debug.Print sbMain.Inputs.Count
    For Each inp In sbMain.Inputs
        Debug.Print inp.Name & inp.Value
    Next
Set sbMain.Inputs = Nothing
End Sub

0
chrislee8Author Commented:
now I understand my problem totally.

Thank you so much

chris lee
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

caraf_gCommented:
In the object world, every time you use the Set keyword, you change the object to which an object variable points.

So

Dim obj1 As SomeClass
Dim obj2 As SomeClass

'At the moment, neither obj1 nor obj2 are pointing at any real object, _
 in other words the statement _
 obj1 Is Nothing _
 will be True. So you can do something like:
If obj1 Is Nothing Then
    'Do Something
End If

'Also, if you now try to use obj1...
obj1.SomeProperty = SomeValue
'oops! now you get the famous Error 91.

'So, we must "instantiate" the objects. _
 That's where the "New" keyword comes in
Set obj1 = New SomeClass

'Now you've created an object of type SomeClass, _
 and object variable obj1 points to this object.
obj1.SomeProperty = "Hi"
msgbox obj1.SomeProperty 'will show "hi". No surprise...

Set obj2 = obj1
'What's happened here?
'We have not instantiated a new object - we didn't use the "New" keyword. So we still only have one object. But now both our object variables point to this object.
msgbox obj2.SomeProperty 'Proof! This also shows "hi".

'Even better, let's change obj2...
obj2.SomeProperty = "Bye"

'And let's look at obj1...
msgbox obj1.SomeProperty 'shows "Bye".

'This "link" is only broken if either of the object variables is made to point to a different object variable:
Set obj1 = New SomeClass 'or...
Set obj1 = AnotherObjectVariableOfTypeSomeClass ':-)
'or even...
Set obj1 = Nothing 'by doing this, you have severed the link between the object in memory and the object variable obj1. obj1 no longer points at any object, and hence "obj1 Is Nothing" (Note: you use "Is", not "="!!!)

What's so bad about dim as New?

Dim objX As New SomeClass
'at this moment in time, objX Is Nothing. But if you refer to objX in whatever way, VB will do:

'"is objX Is Nothing? If yes, quick, do a "Set objX = New SomeClass". This is true even if you the first line you run is
If objX Is Nothing Then
    'This will never happen, before it executes the line, _
     VB will have Set the object variable. Nasty.
End If

'What's even nastier is a do loop.

"Do 10 times"
    objX.SomeProperty = SomeValue
Loop

'First iteration, objX Is Nothing, so VB quickly instantiates it. Any subsequent iteration, Not objX Is Nothing, so VB will leave it alone. So even though you are going through 10 iterations, you're really doing something 10 times to one single object. And that's what tripped you up in your code sample.

I hope this helped you understand the weird and wonderful world of object oriented programming in VB a bit better!

Good luck


Pino
0
caraf_gCommented:
Thank you... don't forget to accept the answer!
0
chrislee8Author Commented:
now I understand my problem totally.

Thank you so much

chris lee
0
caraf_gCommented:
Thanks again, no problem. Please accept the answer ;-)
0
caraf_gCommented:
Ah, my apologies, you just did :o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.