473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

List.Add method overwriting collectionbase?

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.
Feb 26 '06 #1
8 3910
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

"Yuk Tang" <ji********@yahoo.com> wrote in message
news:Xn*******************************@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.

Feb 26 '06 #2
"Terry Burns" <me@mine.com> wrote in
news:Oj**************@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.
Feb 26 '06 #3
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
"Yuk Tang" <ji********@yahoo.com> wrote in message
news:Xn*******************************@130.133.1.4 ...
"Terry Burns" <me@mine.com> wrote in
news:Oj**************@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.

Feb 26 '06 #4
"Terry Burns" <me@mine.com> wrote in
news:ek*************@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.
Feb 26 '06 #5
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
Feb 26 '06 #6
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
"Yuk Tang" <ji********@yahoo.com> wrote in message
news:Xn*******************************@130.133.1.4 ...
"Terry Burns" <me@mine.com> wrote in
news:ek*************@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.

Feb 26 '06 #7
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in
news:#d**************@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.
Feb 26 '06 #8
"Terry Burns" <me@mine.com> wrote in
news:#5**************@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.
Feb 26 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Colin Basterfield | last post by:
Hi, I have a list which is derived from CollectionBase, and it contains a list of User objects, which I want to Serialize out to an XML file. Is there anywhere where I can find how to decode...
7
by: Christopher Wells | last post by:
There's something I don't understand about how the CollectionBase class is defined. I can declare: class Derived : CollectionBase { } I can instantiate this Derived class: Derived derived...
3
by: moondaddy | last post by:
I wrote my own List class which I use to bind to list controls. this class inherits CollectionBase and implements IBindingList. This class contains a list of business classes such as customers...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
5
by: Tony | last post by:
Hello! Here I have a collection class Cards which is derived from the Base class CollectionBase. This class Cards is a container for Card object. Now to my question at the bottom of this class...
3
by: Tony Johansson | last post by:
Hello! Sorry for opening up this task again. I want to fully understand this List that is return from CollectionBase. According to you is List in CollectionBase implemented something like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.