List.Add method overwriting collectionbase?

Y

Yuk Tang

I am tearing my hair out over this, since I can't see what I'm doing
wrong (duh, if I knew, I wouldn't be asking the question). I am
adding Field items to a Field Collection, but for some reason it
wants to start from the beginning and overwrite all entries before
adding the latest member. I've added a couple of msgboxes to
illustrate this, one at the add method, another cycling through the
collection after the addition has been made.

To sample the code, c&p it into a class library, dim a TestField and
activate the Testfield.Something method.


---------------------
The results were:

[add method]
0:hello
[cycle]
Field0:hello

[add method]
1:world
[cycle]
Field0:world
Field1:world

[add method]
2:murder
[cycle]
Field0:murder
Field1:murder
Field2:murder



--------------------------

Public Class TestField
Public Class Field
Private mstrName As String
Property Name() As String
Get
Return mstrName
End Get
Set(ByVal Value As String)
mstrName = Value
End Set
End Property
End Class

Public Class FieldCollection
Inherits System.Collections.CollectionBase
Public ReadOnly Property Item(ByVal index As Integer) As Field
Get
Return CType(List.Item(index), Field)
End Get
End Property
Public Sub Add(ByVal aField As Field)
MsgBox(Str(Count) + ":" + aField.Name)
list.Add(aField)
End Sub
Public Sub Remove(ByVal index As Integer)
List.RemoveAt(index)
End Sub
End Class

Sub Something()
Dim afield As New Field
Dim aFieldCollection As New FieldCollection
Dim astring As String
Dim a, i As Integer
For a = 1 To 3
Select Case a
Case 1
astring = "hello"
Case 2
astring = "world"
Case 3
astring = "murder"
End Select
afield.name = astring
aFieldCollection.Add(afield)
If aFieldCollection.Count > 0 Then
For i = 0 To aFieldCollection.Count - 1
MsgBox("Field" + Str(i) + ":" + _
aFieldCollection.Item(i).Name)
Next i
End If
Next a
End Sub
End Class
 
T

Terry Burns

This is working exactly as you have coded it. Each time you add an item you
cycle thru the entire list. What were you expecting to see in your results ?
 
Y

Yuk Tang

Terry Burns said:
This is working exactly as you have coded it. Each time you add an
item you cycle thru the entire list. What were you expecting to
see in your results ?

I was hoping to just add an item to the end of the collection.
 
T

Terry Burns

I must be missing something here, that what it looks like you are doing,
what makes you think you are overwriting the collection>?
 
Y

Yuk Tang

Terry Burns said:
I must be missing something here, that what it looks like you are
doing, what makes you think you are overwriting the collection?

I added msgboxes to illustrate the contents of the collection, one in
the add method itself just before the addition, another just after
the addition, cycling through the collection.
If aFieldCollection.Count > 0 Then
For i = 0 To aFieldCollection.Count - 1
MsgBox("Field" + Str(i) + ":" + _
aFieldCollection.Item(i).Name)
Next i
End If

The above goes through various values of i from 0 to the end, and
states what the name property of that particular item is. The last
round of results was
Field0:murder
Field1:murder
Field2:murder

when it should have been
 
C

Cor Ligthert [MVP]

Yuk,

Can you add the Line I have add inline.
If you don't understand it, than reply than I will tell, however I assume
that you see it.
astring = "world"
Case 3
astring = "murder"
End Select

afield = New Field
afield.name = astring
aFieldCollection.Add(afield)
If aFieldCollection.Count > 0 Then
For i = 0 To aFieldCollection.Count - 1
MsgBox("Field" + Str(i) + ":" + _

I hope this helps,

Cor
 
T

Terry Burns

OK Now i see it., I tried it out and used debug, here is the solution



Public Class TestField

Public Class Field

Private mstrName As String

Property Name() As String

Get

Return mstrName

End Get

Set(ByVal Value As String)

mstrName = Value

End Set

End Property

End Class

Public Class FieldCollection

Inherits System.Collections.CollectionBase

Public ReadOnly Property Item(ByVal index As Integer) As Field

Get

Return CType(List.Item(index), Field)

End Get

End Property

Public Sub Add(ByVal aField As Field)

Debug.WriteLine(Str(Count) + ":" + aField.Name)

list.Add(aField)

End Sub

Public Sub Remove(ByVal index As Integer)

List.RemoveAt(index)

End Sub

End Class

Public Sub Something()

Dim afield As New Field

Dim aFieldCollection As New FieldCollection

Dim astring As String

Dim a, i As Integer

For a = 1 To 3

Select Case a

Case 1

astring = "hello"

Case 2

astring = "world"

Case 3

astring = "murder"

End Select

'**** here it is ************

'Needed to create a new object of type Field

afield = New Field

afield.Name = astring

aFieldCollection.Add(afield)

If aFieldCollection.Count > 0 Then

For i = 0 To aFieldCollection.Count - 1

Debug.WriteLine("Field" + Str(i) + ":" + aFieldCollection.Item(i).Name)

Next i

End If

Next a

End Sub

End Class
 
Y

Yuk Tang

Cor Ligthert said:
Yuk,

Can you add the Line I have add inline.
If you don't understand it, than reply than I will tell, however I
assume that you see it.


afield = New Field


I hope this helps,

Cor

Eureka! Thanks for the help.
 
Y

Yuk Tang

Terry Burns said:
OK Now i see it., I tried it out and used debug, here is the
solution

'**** here it is ************

'Needed to create a new object of type Field

afield = New Field

afield.Name = astring

aFieldCollection.Add(afield)

Thanks for that. The thing is, I did have that in my original code
(along with afield=nothing at the end), but took it out to improve
performance. Doh!
 

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