Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection byRef? 1

Status
Not open for further replies.

stnkyminky

Programmer
Joined
Oct 15, 2001
Messages
476
Location
US
First off let me say that anyone who helps explain why this is happening will receive a star.

I've got several classes(Publication and Ad). The publication class contains information about a trade magazine(name, issue date). The Ad class contains information about an Ad that is in a magazine(name, products mentioned, etc)

I have a public property in the Publication class that is an Ad Collection (Ads). When I populate the publication class, I also load all of the ads in that issue. I have a function that returns an Ad Collection (Ads) and it is assigned in a For Loop ----->
Code:
pub(i).Ads = GetAds(pub(i).ID)

I then take the Publication Collection (Publications) and add the objects to a combo box via For Each loop
Code:
dim pub as Publication
dim pubs as Publications
pubs = GetPublications()
for each pub in pubs
ComboBox1.Items.Add(pub)

Within the GetPublications function was a call to a separate sub that would add the Ads to the Publication Object

Code:
          count = col.Count - 1
            For i = 0 To count
                col(i).Ads = obj.getAds(col(i).ID)
            Next


Now, when the user clicks on a publication (SelectedIndexChange Event), I take the SelectedItem in the combo box and iterate through the Ads and add them to a separate combo box
Code:
  cboItem.Items.Clear()
            _ads = CType(cboPub.SelectedItem, LeadSystem.BAL.Publication).Ads

            If _ads.Count > 0 Then
                For i = 0 To count
                    cboItem.Items.Add(_ads(i))
                Next
            Else
                cboItem.Enabled = False
                cboItem.Items.Add("No Ads Available")
            End If
            cboItem.SelectedIndex = 0

The code above existed in Try..Catch. In the Finally statement I would do the following
Code:
_ads.clear()
_ads = Nothing

Clicking through each item in the publication combo box worked great and returned the ads in that publication. After reaching the last item, I then tried to access the first item in the publication combo. At that point the Ads collection in the Publication object was Nothing. I wasn't altering the code anywhere in the form.

Just as a stab to fix the problem. I removed code that would "Clear" the collection (code above). After commenting out that code, the ads are retained. My questions is this.

Scott
Programmer Analyst
<{{><
 
Hmmmm. Your question is what? Left that part out!
 
Wow....guess I was trying to give too much detail and forgot the important things.

When the code below is commented I don't have problems. Otherwise I'm able to cycle through the Ad objects in the Publication once and then the Ad objects are equal to nothing.
Code:
_ads.clear()
_ads = Nothing

Scott
Programmer Analyst
<{{><
 
You voluntarily wiped out the original collection.
_ads conatins a reference to the original collection.

ads = CType(cboPub.SelectedItem, LeadSystem.BAL.Publication).Ads

therfore _ads.clear() is the same as saying

CType(cboPub.SelectedItem, LeadSystem.BAL.Publication).Ads.Clear

The _ads = Nothing is ok because it only sets a variable containig a reference to the collection to nothing.



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top