Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

collection and object question

Posted on 1999-07-12
8
Medium Priority
?
183 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:chrislee8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 80 total points
ID: 1523779
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523780
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
 

Author Comment

by:chrislee8
ID: 1523781
now I understand my problem totally.

Thank you so much

chris lee
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:caraf_g
ID: 1523782
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523783
Thank you... don't forget to accept the answer!
0
 

Author Comment

by:chrislee8
ID: 1523784
now I understand my problem totally.

Thank you so much

chris lee
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523785
Thanks again, no problem. Please accept the answer ;-)
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523786
Ah, my apologies, you just did :o)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question