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
--
Cheers, ymt.