Home All Groups Group Topic Archive Search About

List.Add method overwriting collectionbase?

Author
26 Feb 2006 1:22 PM
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

--
Cheers, ymt.

Author
26 Feb 2006 1:32 PM
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 ?

--
Terry Burns
http://TrainingOn.net

Show quoteHide quote
"Yuk Tang" <jim.lak***@yahoo.com> wrote in message
news:Xns97768816E808Djimlaker2yahoocom@130.133.1.4...
>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.
Author
26 Feb 2006 1:45 PM
Yuk Tang
"Terry Burns" <m*@mine.com> wrote in
news:OjBu9ktOGHA.3360@TK2MSFTNGP09.phx.gbl:
>
> 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.


--
Cheers, ymt.
Author
26 Feb 2006 2:04 PM
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>?

--
Terry Burns
http://TrainingOn.net
Show quoteHide quote
"Yuk Tang" <jim.lak***@yahoo.com> wrote in message
news:Xns97768BEB2788Djimlaker2yahoocom@130.133.1.4...
> "Terry Burns" <m*@mine.com> wrote in
> news:OjBu9ktOGHA.3360@TK2MSFTNGP09.phx.gbl:
>>
>> 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.
>
>
> --
> Cheers, ymt.
Author
26 Feb 2006 2:15 PM
Yuk Tang
"Terry Burns" <m*@mine.com> wrote in
news:ekEOx2tOGHA.312@TK2MSFTNGP12.phx.gbl:
>
> 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

> Field0:hello
> Field1:world
> Field2:murder


--
Cheers, ymt.
Author
26 Feb 2006 3:56 PM
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


--
Terry Burns
http://TrainingOn.net
Show quoteHide quote
"Yuk Tang" <jim.lak***@yahoo.com> wrote in message
news:Xns977690FC81A0Cjimlaker2yahoocom@130.133.1.4...
> "Terry Burns" <m*@mine.com> wrote in
> news:ekEOx2tOGHA.312@TK2MSFTNGP12.phx.gbl:
>>
>> 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
>
>> Field0:hello
>> Field1:world
>> Field2:murder
>
>
> --
> Cheers, ymt.
Author
26 Feb 2006 4:44 PM
Yuk Tang
Show quote Hide quote
"Terry Burns" <m*@mine.com> wrote in
news:#5cRj1uOGHA.3196@TK2MSFTNGP09.phx.gbl:
>
> 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!


--
Cheers, ymt.
Author
26 Feb 2006 3:24 PM
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
Author
26 Feb 2006 3:59 PM
Yuk Tang
Show quote Hide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in
news:#dZs0huOGHA.1192@TK2MSFTNGP11.phx.gbl:
>
> 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

Eureka!  Thanks for the help.


--
Cheers, ymt.