Link to home
Start Free TrialLog in
Avatar of chrislee8
chrislee8

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of caraf_g
caraf_g

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

Avatar of chrislee8

ASKER

now I understand my problem totally.

Thank you so much

chris lee
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
Thank you... don't forget to accept the answer!
now I understand my problem totally.

Thank you so much

chris lee
Thanks again, no problem. Please accept the answer ;-)
Ah, my apologies, you just did :o)