|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
List.Add method overwriting collectionbase?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. 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 ? 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. "Terry Burns" <m*@mine.com> wrote in I was hoping to just add an item to the end of the collection.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 ? -- Cheers, ymt. I must be missing something here, that what it looks like you are doing,
what makes you think you are overwriting the collection>? 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. "Terry Burns" <m*@mine.com> wrote in I added msgboxes to illustrate the contents of the collection, one 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? the add method itself just before the addition, another just after the addition, cycling through the collection. > If aFieldCollection.Count > 0 Then The above goes through various values of i from 0 to the end, and > For i = 0 To aFieldCollection.Count - 1 > MsgBox("Field" + Str(i) + ":" + _ > aFieldCollection.Item(i).Name) > Next i > End If states what the name property of that particular item is. The last round of results was > Field0:murder when it should have been> Field1:murder > Field2:murder > Field0:hello > Field1:world > Field2:murder -- Cheers, ymt. 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 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.
Show quote
Hide quote
"Terry Burns" <m*@mine.com> wrote in Thanks for that. The thing is, I did have that in my original code 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) (along with afield=nothing at the end), but took it out to improve performance. Doh! -- Cheers, ymt. 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" afield = New Field> Case 3 > astring = "murder" > End Select > afield.name = astring I hope this helps,> aFieldCollection.Add(afield) > If aFieldCollection.Count > 0 Then > For i = 0 To aFieldCollection.Count - 1 > MsgBox("Field" + Str(i) + ":" + _ Cor
Show quote
Hide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in Eureka! Thanks for the help.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 -- Cheers, ymt.
How does the community rate .NET 2005 vs. .NET 2003?
Some thoughts on VB9 Set a text in a RowHeader cell academic version [2003] How to move a menu item to the left?! The variable 'GroupBox1' is either undeclared or was never assigned. List all public properties of a Class ? at run time database connection Moving a project to a different location Using 'Function Keys' In vb.net |
|||||||||||||||||||||||