array of objects changing unusually

G

Guest

hi,
i have an array of 30 adgroup objects, called arrAdGroup. i am using a loop
to traverse through the array to set the adgroup.id property to a value
(which is different every time).
the code sets the value of all the objects in the array to the value of the
last object.
the code is below:

For i = 0 To counter - 1

With oKeyword
..adGroupId = adgroupID(i)
..criterionType =
criterion.com.google.adwords.CriterionType.Keyword
'.criterionTypeSpecified = True
If BidChange(i) Then
..maxCpc = Bid(i)
..maxCpcSpecified = True
End If
Select Case keywordType(i)
Case 1
..type = criterion.com.google.adwords.KeywordType.Broad
Case 2
..type = criterion.com.google.adwords.KeywordType.Exact
Case 3
..type =
criterion.com.google.adwords.KeywordType.Phrase
End Select
If DURLChange(i) Then
..destinationUrl = destinationURL(i)
End If
If paused(i) Then
..paused = True
..pausedSpecified = True
End If
..id = keywordID(i)

End With

' here is where the code is breaking.
If keywordID(i) > 0 Then
arrKeyword(i) = oKeyword
End If
' every time this code executes, the previous object that was set in the
earlier 'loop is overwritten and a new instance of the object appears in the
(i) instance of 'the array.
Next

please help.
 
J

Jack Jackson

You only have one instance of oKeyword. Each time through the loop
you set various properties of oKeyword, and then at the end you set
arrKeyword(i) = oKeyword.

Each arrKeyword() entry has a reference to the same oKeyword object.
You need to allocate a new oKeyword object each time through the loop.

Also, since you do nothing useful in the For loop if keywordID(i) <= 0
it would be more efficient to put this right after the For:

If keywordID(i) <= 0 Then
Continue For
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top