List.Add method overwriting collectionbase?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yuk Tang

    #1

    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.Somet hing 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.Collecti ons.CollectionB ase
    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(Coun t) + ":" + aField.Name)
    list.Add(aField )
    End Sub
    Public Sub Remove(ByVal index As Integer)
    List.RemoveAt(i ndex)
    End Sub
    End Class

    Sub Something()
    Dim afield As New Field
    Dim aFieldCollectio n 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
    aFieldCollectio n.Add(afield)
    If aFieldCollectio n.Count > 0 Then
    For i = 0 To aFieldCollectio n.Count - 1
    MsgBox("Field" + Str(i) + ":" + _
    aFieldCollectio n.Item(i).Name)
    Next i
    End If
    Next a
    End Sub
    End Class

    --
    Cheers, ymt.
  • Terry Burns

    #2
    Re: List.Add method overwriting collectionbase?

    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


    "Yuk Tang" <jim.laker2@yah oo.com> wrote in message
    news:Xns9776881 6E808Djimlaker2 yahoocom@130.13 3.1.4...[color=blue]
    >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.Somet hing 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.Collecti ons.CollectionB ase
    > 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(Coun t) + ":" + aField.Name)
    > list.Add(aField )
    > End Sub
    > Public Sub Remove(ByVal index As Integer)
    > List.RemoveAt(i ndex)
    > End Sub
    > End Class
    >
    > Sub Something()
    > Dim afield As New Field
    > Dim aFieldCollectio n 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
    > aFieldCollectio n.Add(afield)
    > If aFieldCollectio n.Count > 0 Then
    > For i = 0 To aFieldCollectio n.Count - 1
    > MsgBox("Field" + Str(i) + ":" + _
    > aFieldCollectio n.Item(i).Name)
    > Next i
    > End If
    > Next a
    > End Sub
    > End Class
    >
    > --
    > Cheers, ymt.[/color]


    Comment

    • Yuk Tang

      #3
      Re: List.Add method overwriting collectionbase?

      "Terry Burns" <me@mine.com> wrote in
      news:OjBu9ktOGH A.3360@TK2MSFTN GP09.phx.gbl:[color=blue]
      >
      > 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 ?[/color]

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


      --
      Cheers, ymt.

      Comment

      • Terry Burns

        #4
        Re: List.Add method overwriting collectionbase?

        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

        "Yuk Tang" <jim.laker2@yah oo.com> wrote in message
        news:Xns97768BE B2788Djimlaker2 yahoocom@130.13 3.1.4...[color=blue]
        > "Terry Burns" <me@mine.com> wrote in
        > news:OjBu9ktOGH A.3360@TK2MSFTN GP09.phx.gbl:[color=green]
        >>
        >> 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 ?[/color]
        >
        > I was hoping to just add an item to the end of the collection.
        >
        >
        > --
        > Cheers, ymt.[/color]


        Comment

        • Yuk Tang

          #5
          Re: List.Add method overwriting collectionbase?

          "Terry Burns" <me@mine.com> wrote in
          news:ekEOx2tOGH A.312@TK2MSFTNG P12.phx.gbl:[color=blue]
          >
          > I must be missing something here, that what it looks like you are
          > doing, what makes you think you are overwriting the collection?[/color]

          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.
          [color=blue]
          > If aFieldCollectio n.Count > 0 Then
          > For i = 0 To aFieldCollectio n.Count - 1
          > MsgBox("Field" + Str(i) + ":" + _
          > aFieldCollectio n.Item(i).Name)
          > Next i
          > End If[/color]

          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
          [color=blue]
          > Field0:murder
          > Field1:murder
          > Field2:murder[/color]

          when it should have been
          [color=blue]
          > Field0:hello
          > Field1:world
          > Field2:murder[/color]


          --
          Cheers, ymt.

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: List.Add method overwriting collectionbase?

            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.
            [color=blue]
            > astring = "world"
            > Case 3
            > astring = "murder"
            > End Select[/color]

            afield = New Field
            [color=blue]
            > afield.name = astring
            > aFieldCollectio n.Add(afield)
            > If aFieldCollectio n.Count > 0 Then
            > For i = 0 To aFieldCollectio n.Count - 1
            > MsgBox("Field" + Str(i) + ":" + _[/color]

            I hope this helps,

            Cor


            Comment

            • Terry Burns

              #7
              Re: List.Add method overwriting collectionbase?

              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.Collecti ons.CollectionB ase

              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(i ndex)

              End Sub

              End Class

              Public Sub Something()

              Dim afield As New Field

              Dim aFieldCollectio n 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

              aFieldCollectio n.Add(afield)

              If aFieldCollectio n.Count > 0 Then

              For i = 0 To aFieldCollectio n.Count - 1

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

              Next i

              End If

              Next a

              End Sub

              End Class


              --
              Terry Burns

              "Yuk Tang" <jim.laker2@yah oo.com> wrote in message
              news:Xns977690F C81A0Cjimlaker2 yahoocom@130.13 3.1.4...[color=blue]
              > "Terry Burns" <me@mine.com> wrote in
              > news:ekEOx2tOGH A.312@TK2MSFTNG P12.phx.gbl:[color=green]
              >>
              >> I must be missing something here, that what it looks like you are
              >> doing, what makes you think you are overwriting the collection?[/color]
              >
              > 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.
              >[color=green]
              >> If aFieldCollectio n.Count > 0 Then
              >> For i = 0 To aFieldCollectio n.Count - 1
              >> MsgBox("Field" + Str(i) + ":" + _
              >> aFieldCollectio n.Item(i).Name)
              >> Next i
              >> End If[/color]
              >
              > 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
              >[color=green]
              >> Field0:murder
              >> Field1:murder
              >> Field2:murder[/color]
              >
              > when it should have been
              >[color=green]
              >> Field0:hello
              >> Field1:world
              >> Field2:murder[/color]
              >
              >
              > --
              > Cheers, ymt.[/color]


              Comment

              • Yuk Tang

                #8
                Re: List.Add method overwriting collectionbase?

                "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in
                news:#dZs0huOGH A.1192@TK2MSFTN GP11.phx.gbl:[color=blue]
                >
                > 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.
                >[color=green]
                >> astring = "world"
                >> Case 3
                >> astring = "murder"
                >> End Select[/color]
                >
                > afield = New Field
                >[color=green]
                >> afield.name = astring
                >> aFieldCollectio n.Add(afield)
                >> If aFieldCollectio n.Count > 0 Then
                >> For i = 0 To aFieldCollectio n.Count - 1
                >> MsgBox("Field" + Str(i) + ":" + _[/color]
                >
                > I hope this helps,
                >
                > Cor[/color]

                Eureka! Thanks for the help.


                --
                Cheers, ymt.

                Comment

                • Yuk Tang

                  #9
                  Re: List.Add method overwriting collectionbase?

                  "Terry Burns" <me@mine.com> wrote in
                  news:#5cRj1uOGH A.3196@TK2MSFTN GP09.phx.gbl:[color=blue]
                  >
                  > 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
                  >
                  > aFieldCollectio n.Add(afield)[/color]

                  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.

                  Comment

                  Working...