Help with sorting an Arraylist that contains Structures

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

    Help with sorting an Arraylist that contains Structures

    Ok, I give up. I can't seem to construct a decent (productive) way of
    sorting my arraylist.

    I have a structure of two elements:

    Structure TabStructure
    Dim TabName As String
    Dim FullFilePath As String
    End Structure

    Then for example I:

    Dim tab as New TabStructure

    some code

    Then I add "tab" to an arraylist.

    At some point I loop through my arraylist, grab data and create tabs from
    it. Of course the resulting list of tabs are out of sequence.

    Can someone help me to sort my array list alphabetically by
    TabStructure.Ta bName?

    I would greatly appreciate it.

  • =?Utf-8?B?S2VycnkgTW9vcm1hbg==?=

    #2
    RE: Help with sorting an Arraylist that contains Structures

    Justin,

    One option is to have your structure implement the IComparable interface:

    Structure TabStructure
    Implements IComparable(Of TabStructure)
    Dim TabName As String
    Dim FullFilePath As String

    Function CompareTo(ByVal other As TabStructure) As Integer
    Implements IComparable(Of TabStructure).C ompareTo

    Return Me.TabName.Comp areTo(other.Tab Name)

    End Function

    End Structure

    Now ArrayList.Sort should sort the "tabs" by TabName.

    By the way, I would use a class instead of a structure.

    Kerry Moorman

    "Justin" wrote:
    Ok, I give up. I can't seem to construct a decent (productive) way of
    sorting my arraylist.
    >
    I have a structure of two elements:
    >
    Structure TabStructure
    Dim TabName As String
    Dim FullFilePath As String
    End Structure
    >
    Then for example I:
    >
    Dim tab as New TabStructure
    >
    some code
    >
    Then I add "tab" to an arraylist.
    >
    At some point I loop through my arraylist, grab data and create tabs from
    it. Of course the resulting list of tabs are out of sequence.
    >
    Can someone help me to sort my array list alphabetically by
    TabStructure.Ta bName?
    >
    I would greatly appreciate it.
    >
    >

    Comment

    • Bill McCarthy

      #3
      Re: Help with sorting an Arraylist that contains Structures

      Hi Justin,

      First, why are you using ArrayList ? You should use the generic List, eg.

      Dim myList as New List(Of TabStructure)

      Then, assuming you are using VB 2008, you can pass in a lambda function to
      the sort method:

      myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )







      "Justin" <None@None.comw rote in message
      news:OEUepa%23A JHA.1828@TK2MSF TNGP03.phx.gbl. ..
      Ok, I give up. I can't seem to construct a decent (productive) way of
      sorting my arraylist.
      >
      I have a structure of two elements:
      >
      Structure TabStructure
      Dim TabName As String
      Dim FullFilePath As String
      End Structure
      >
      Then for example I:
      >
      Dim tab as New TabStructure
      >
      some code
      >
      Then I add "tab" to an arraylist.
      >
      At some point I loop through my arraylist, grab data and create tabs from
      it. Of course the resulting list of tabs are out of sequence.
      >
      Can someone help me to sort my array list alphabetically by
      TabStructure.Ta bName?
      >
      I would greatly appreciate it.

      Comment

      • Justin

        #4
        Re: Help with sorting an Arraylist that contains Structures

        First, until just now I never heard of "lists". However you mention VS2008
        specifically and while I am using VS2008 I'm wanting to stay with in the
        confines of framework 2. Does your suggestion do that? If so then I'll
        read up on it.

        Googling "VB.NET list" came up with nothing but arraylists and listboxes.

        I already know arraylists and I already know how to check for dups before
        adding so it was pretty simple to bang out the code. This arraylist is used
        all over my app.



        "Bill McCarthy" <Bill@localhost .comwrote in message
        news:127A8FEC-F7C3-4C20-A160-323A34539761@mi crosoft.com...
        Hi Justin,
        >
        First, why are you using ArrayList ? You should use the generic List, eg.
        >
        Dim myList as New List(Of TabStructure)
        >
        Then, assuming you are using VB 2008, you can pass in a lambda function to
        the sort method:
        >
        myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )
        >
        >
        >
        >
        >
        >
        >
        "Justin" <None@None.comw rote in message
        news:OEUepa%23A JHA.1828@TK2MSF TNGP03.phx.gbl. ..
        >Ok, I give up. I can't seem to construct a decent (productive) way of
        >sorting my arraylist.
        >>
        >I have a structure of two elements:
        >>
        >Structure TabStructure
        > Dim TabName As String
        > Dim FullFilePath As String
        >End Structure
        >>
        >Then for example I:
        >>
        >Dim tab as New TabStructure
        >>
        >some code
        >>
        >Then I add "tab" to an arraylist.
        >>
        >At some point I loop through my arraylist, grab data and create tabs from
        >it. Of course the resulting list of tabs are out of sequence.
        >>
        >Can someone help me to sort my array list alphabetically by
        >TabStructure.T abName?
        >>
        >I would greatly appreciate it.
        >

        Comment

        • Justin

          #5
          Re: Help with sorting an Arraylist that contains Structures

          Eh, this is my only instance and it was just 4 lines of code. I was
          wondering how to implement IComparable. Now I get it. This make much more
          sense then what I was reading earlier today.

          Thanks! I'll play with this tomorrow.


          "Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.comwrote in message
          news:36757DFF-6898-4C5F-BA04-08CB80DE6E94@mi crosoft.com...
          Justin,
          >
          One option is to have your structure implement the IComparable interface:
          >
          Structure TabStructure
          Implements IComparable(Of TabStructure)
          Dim TabName As String
          Dim FullFilePath As String
          >
          Function CompareTo(ByVal other As TabStructure) As Integer
          Implements IComparable(Of TabStructure).C ompareTo
          >
          Return Me.TabName.Comp areTo(other.Tab Name)
          >
          End Function
          >
          End Structure
          >
          Now ArrayList.Sort should sort the "tabs" by TabName.
          >
          By the way, I would use a class instead of a structure.
          >
          Kerry Moorman
          >
          "Justin" wrote:
          >
          >Ok, I give up. I can't seem to construct a decent (productive) way of
          >sorting my arraylist.
          >>
          >I have a structure of two elements:
          >>
          >Structure TabStructure
          > Dim TabName As String
          > Dim FullFilePath As String
          >End Structure
          >>
          >Then for example I:
          >>
          >Dim tab as New TabStructure
          >>
          >some code
          >>
          >Then I add "tab" to an arraylist.
          >>
          >At some point I loop through my arraylist, grab data and create tabs from
          >it. Of course the resulting list of tabs are out of sequence.
          >>
          >Can someone help me to sort my array list alphabetically by
          >TabStructure.T abName?
          >>
          >I would greatly appreciate it.
          >>
          >>

          Comment

          • Jack Jackson

            #6
            Re: Help with sorting an Arraylist that contains Structures

            The generic List(Of T) is in 2.0. Its advantage over ArrayList is
            that the contents of the list is typed so you don't have to cast the
            contents and therefore avoid runtime errors if you make a mistake
            casting the contents. I also strongly urge you to use it instead of
            ArrayList.

            On Thu, 21 Aug 2008 20:48:26 -0700, "Justin" <None@None.comw rote:
            >First, until just now I never heard of "lists". However you mention VS2008
            >specifically and while I am using VS2008 I'm wanting to stay with in the
            >confines of framework 2. Does your suggestion do that? If so then I'll
            >read up on it.
            >
            >Googling "VB.NET list" came up with nothing but arraylists and listboxes.
            >
            >I already know arraylists and I already know how to check for dups before
            >adding so it was pretty simple to bang out the code. This arraylist is used
            >all over my app.
            >
            >
            >
            >"Bill McCarthy" <Bill@localhost .comwrote in message
            >news:127A8FE C-F7C3-4C20-A160-323A34539761@mi crosoft.com...
            >Hi Justin,
            >>
            >First, why are you using ArrayList ? You should use the generic List, eg.
            >>
            > Dim myList as New List(Of TabStructure)
            >>
            >Then, assuming you are using VB 2008, you can pass in a lambda function to
            >the sort method:
            >>
            > myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )
            >>
            >>
            >>
            >>
            >>
            >>
            >>
            >"Justin" <None@None.comw rote in message
            >news:OEUepa%23 AJHA.1828@TK2MS FTNGP03.phx.gbl ...
            >>Ok, I give up. I can't seem to construct a decent (productive) way of
            >>sorting my arraylist.
            >>>
            >>I have a structure of two elements:
            >>>
            >>Structure TabStructure
            >> Dim TabName As String
            >> Dim FullFilePath As String
            >>End Structure
            >>>
            >>Then for example I:
            >>>
            >>Dim tab as New TabStructure
            >>>
            >>some code
            >>>
            >>Then I add "tab" to an arraylist.
            >>>
            >>At some point I loop through my arraylist, grab data and create tabs from
            >>it. Of course the resulting list of tabs are out of sequence.
            >>>
            >>Can someone help me to sort my array list alphabetically by
            >>TabStructure. TabName?
            >>>
            >>I would greatly appreciate it.
            >>

            Comment

            • Justin

              #7
              Re: Help with sorting an Arraylist that contains Structures

              Cool, I'll check it out. Thanks.


              "Jack Jackson" <jjackson@cinno vations.netwrot e in message
              news:c0jsa4hrrf chq8o584o27cule 3p5d6gp6s@4ax.c om...
              The generic List(Of T) is in 2.0. Its advantage over ArrayList is
              that the contents of the list is typed so you don't have to cast the
              contents and therefore avoid runtime errors if you make a mistake
              casting the contents. I also strongly urge you to use it instead of
              ArrayList.
              >
              On Thu, 21 Aug 2008 20:48:26 -0700, "Justin" <None@None.comw rote:
              >
              >>First, until just now I never heard of "lists". However you mention
              >>VS2008
              >>specificall y and while I am using VS2008 I'm wanting to stay with in the
              >>confines of framework 2. Does your suggestion do that? If so then I'll
              >>read up on it.
              >>
              >>Googling "VB.NET list" came up with nothing but arraylists and listboxes.
              >>
              >>I already know arraylists and I already know how to check for dups before
              >>adding so it was pretty simple to bang out the code. This arraylist is
              >>used
              >>all over my app.
              >>
              >>
              >>
              >>"Bill McCarthy" <Bill@localhost .comwrote in message
              >>news:127A8F EC-F7C3-4C20-A160-323A34539761@mi crosoft.com...
              >>Hi Justin,
              >>>
              >>First, why are you using ArrayList ? You should use the generic List,
              >>eg.
              >>>
              >> Dim myList as New List(Of TabStructure)
              >>>
              >>Then, assuming you are using VB 2008, you can pass in a lambda function
              >>to
              >>the sort method:
              >>>
              >> myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )
              >>>
              >>>
              >>>
              >>>
              >>>
              >>>
              >>>
              >>"Justin" <None@None.comw rote in message
              >>news:OEUepa%2 3AJHA.1828@TK2M SFTNGP03.phx.gb l...
              >>>Ok, I give up. I can't seem to construct a decent (productive) way of
              >>>sorting my arraylist.
              >>>>
              >>>I have a structure of two elements:
              >>>>
              >>>Structure TabStructure
              >>> Dim TabName As String
              >>> Dim FullFilePath As String
              >>>End Structure
              >>>>
              >>>Then for example I:
              >>>>
              >>>Dim tab as New TabStructure
              >>>>
              >>>some code
              >>>>
              >>>Then I add "tab" to an arraylist.
              >>>>
              >>>At some point I loop through my arraylist, grab data and create tabs
              >>>from
              >>>it. Of course the resulting list of tabs are out of sequence.
              >>>>
              >>>Can someone help me to sort my array list alphabetically by
              >>>TabStructure .TabName?
              >>>>
              >>>I would greatly appreciate it.
              >>>

              Comment

              • Bill McCarthy

                #8
                Re: Help with sorting an Arraylist that contains Structures

                Hi Justin,

                List(Of T) basically replaces ArrayList as of .NET 2.0//VB 2005. It uses
                generics, so the item type is strongly typed. With value types, such as your
                structure, this avoids costly boxing and unboxing the arraylist has to
                perform. So you get more robust code and in many cases a bit of a
                performance improvement too.

                As to compiling against the 2.0 framework, in VB 2008 in project options,
                compile tab, select Advanced Compile Options and set the target framework to
                2.0, 3.0 or 3.5. If you are using features not supported by the target
                framework, VB will warn you. The example of the lambda function I gave
                should compile to 2.0 without problem. The lambda function, is basically an
                inline function.

                As to generics, it's been a while since I wrote much about them, but perhaps
                this old article may help introduce you to them:




                "Justin" <None@None.comw rote in message
                news:O$BbvnABJH A.3748@TK2MSFTN GP03.phx.gbl...
                First, until just now I never heard of "lists". However you mention
                VS2008 specifically and while I am using VS2008 I'm wanting to stay with
                in the confines of framework 2. Does your suggestion do that? If so then
                I'll read up on it.
                >
                Googling "VB.NET list" came up with nothing but arraylists and listboxes.
                >
                I already know arraylists and I already know how to check for dups before
                adding so it was pretty simple to bang out the code. This arraylist is
                used all over my app.
                >
                >
                >
                "Bill McCarthy" <Bill@localhost .comwrote in message
                news:127A8FEC-F7C3-4C20-A160-323A34539761@mi crosoft.com...
                >Hi Justin,
                >>
                >First, why are you using ArrayList ? You should use the generic List,
                >eg.
                >>
                > Dim myList as New List(Of TabStructure)
                >>
                >Then, assuming you are using VB 2008, you can pass in a lambda function
                >to the sort method:
                >>
                > myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )
                >>
                >>
                >>
                >>
                >>
                >>
                >>
                >"Justin" <None@None.comw rote in message
                >news:OEUepa%23 AJHA.1828@TK2MS FTNGP03.phx.gbl ...
                >>Ok, I give up. I can't seem to construct a decent (productive) way of
                >>sorting my arraylist.
                >>>
                >>I have a structure of two elements:
                >>>
                >>Structure TabStructure
                >> Dim TabName As String
                >> Dim FullFilePath As String
                >>End Structure
                >>>
                >>Then for example I:
                >>>
                >>Dim tab as New TabStructure
                >>>
                >>some code
                >>>
                >>Then I add "tab" to an arraylist.
                >>>
                >>At some point I loop through my arraylist, grab data and create tabs
                >>from it. Of course the resulting list of tabs are out of sequence.
                >>>
                >>Can someone help me to sort my array list alphabetically by
                >>TabStructure. TabName?
                >>>
                >>I would greatly appreciate it.
                >>

                Comment

                • Tom Shelton

                  #9
                  Re: Help with sorting an Arraylist that contains Structures

                  On 2008-08-21, Justin <None@None.comw rote:
                  Ok, I give up. I can't seem to construct a decent (productive) way of
                  sorting my arraylist.
                  >
                  I have a structure of two elements:
                  >
                  Structure TabStructure
                  Dim TabName As String
                  Dim FullFilePath As String
                  End Structure
                  >
                  Then for example I:
                  >
                  Dim tab as New TabStructure
                  >
                  some code
                  >
                  Then I add "tab" to an arraylist.
                  >
                  At some point I loop through my arraylist, grab data and create tabs from
                  it. Of course the resulting list of tabs are out of sequence.
                  >
                  Can someone help me to sort my array list alphabetically by
                  TabStructure.Ta bName?
                  >
                  I would greatly appreciate it.
                  >
                  Others have point out to you the List(Of T) - so I won't rehash that :) But,
                  I'm going to talk about your structure. The fact is, there is almost never
                  any reason to use a structure except for interop scenarios or where you want
                  to preserve value symantics... TabStructure should be a class. This will
                  become apparent as soon as you start trying to modify individual members of
                  your list:

                  Private Structure TStruct
                  Public s1 As String
                  Public s2 As String

                  Public Sub New(ByVal s1 As String, ByVal s2 As String)
                  Me.s1 = s1
                  Me.s2 = s2
                  End Sub
                  End Structure

                  Sub Main()
                  Dim l As New List(Of TStruct)
                  l.Add(New TStruct("one", "one"))
                  l.Add(New TStruct("two", "one"))
                  l.Add(New TStruct("three" , "one"))
                  l.Add(New TStruct("four", "one"))

                  For Each i As TStruct In l
                  i.s2 = "changing.. ."
                  Next

                  For Each i As TStruct In l
                  Console.WriteLi ne("s1={0}, s2={1}", i.s1, i.s2)
                  Next
                  End Sub

                  You might be suprised that the values of s2 changend in the first loop, don't
                  actually change anything. That's because TStruct is a value type, and any
                  time you assign or return it you get a copy and not the original. If I change
                  TStruct to a class:

                  Private Class TStruct
                  Public s1 As String
                  Public s2 As String

                  Public Sub New(ByVal s1 As String, ByVal s2 As String)
                  Me.s1 = s1
                  Me.s2 = s2
                  End Sub
                  End Class

                  Then every thing works as expected.

                  So, again unless your doing interop or really want value symantics, there is
                  not much gained by using a struct over a class.

                  --
                  Tom Shelton

                  Comment

                  • Justin

                    #10
                    Re: Help with sorting an Arraylist that contains Structures

                    Ok, after getting beat in the head with "class" by 4 people I converted it
                    to a class :)

                    I tried to convert my arraylist to a list and that was just an ugly mess.
                    Here's the first two errors I received:
                    'TabsArray' cannot expose type 'TabClass' outside the project through class
                    'frmMain'
                    Too few type arguments to 'System.Collect ions.Generic.Li st(Of T)'

                    But enough of that. I want to wrap my head around iComparable first. I'll
                    deal with that later.

                    I implemented iComparable and now when I try to .sort my arraylist I get:

                    "Failed to compare two elements in the array."

                    Again, thanks for the help everyone!




                    "Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.comwrote in message
                    news:36757DFF-6898-4C5F-BA04-08CB80DE6E94@mi crosoft.com...
                    Justin,
                    >
                    One option is to have your structure implement the IComparable interface:
                    >
                    Structure TabStructure
                    Implements IComparable(Of TabStructure)
                    Dim TabName As String
                    Dim FullFilePath As String
                    >
                    Function CompareTo(ByVal other As TabStructure) As Integer
                    Implements IComparable(Of TabStructure).C ompareTo
                    >
                    Return Me.TabName.Comp areTo(other.Tab Name)
                    >
                    End Function
                    >
                    End Structure
                    >
                    Now ArrayList.Sort should sort the "tabs" by TabName.
                    >
                    By the way, I would use a class instead of a structure.
                    >
                    Kerry Moorman
                    >
                    "Justin" wrote:
                    >
                    >Ok, I give up. I can't seem to construct a decent (productive) way of
                    >sorting my arraylist.
                    >>
                    >I have a structure of two elements:
                    >>
                    >Structure TabStructure
                    > Dim TabName As String
                    > Dim FullFilePath As String
                    >End Structure
                    >>
                    >Then for example I:
                    >>
                    >Dim tab as New TabStructure
                    >>
                    >some code
                    >>
                    >Then I add "tab" to an arraylist.
                    >>
                    >At some point I loop through my arraylist, grab data and create tabs from
                    >it. Of course the resulting list of tabs are out of sequence.
                    >>
                    >Can someone help me to sort my array list alphabetically by
                    >TabStructure.T abName?
                    >>
                    >I would greatly appreciate it.
                    >>
                    >>

                    Comment

                    • Tom Shelton

                      #11
                      Re: Help with sorting an Arraylist that contains Structures

                      On 2008-08-22, Justin <None@None.comw rote:
                      Ok, after getting beat in the head with "class" by 4 people I converted it
                      to a class :)
                      >
                      I tried to convert my arraylist to a list and that was just an ugly mess.
                      Here's the first two errors I received:
                      'TabsArray' cannot expose type 'TabClass' outside the project through class
                      'frmMain'
                      Too few type arguments to 'System.Collect ions.Generic.Li st(Of T)'
                      >
                      But enough of that. I want to wrap my head around iComparable first. I'll
                      deal with that later.
                      >
                      I implemented iComparable and now when I try to .sort my arraylist I get:
                      >
                      "Failed to compare two elements in the array."
                      >
                      Again, thanks for the help everyone!
                      Using a list should be simply:

                      Dim myList As New List(Of TabClass)()

                      from there, it is very much like an ArrayList - only better and faster :)

                      --
                      Tom Shelton

                      Comment

                      • Justin

                        #12
                        Re: Help with sorting an Arraylist that contains Structures

                        I've come across this before. When something as simple as this just isn't
                        working I've learned to start a whole new project and bring everything over.
                        Surprisingly that didn't take too long.

                        The error, "Failed to compare two elements in the array.", came over however
                        once I converted the arraylist to a list the error went away and it's now
                        sorting properly. Obviously the List errors did not come over as well.

                        I'm growing a little tired of having to start whole new projects because of
                        silly unexplained issues but at the same time I'm glad to be able to move
                        forward.

                        Again, thanks to everyone!


                        "Tom Shelton" <tom_shelton@co mcastXXXXXXX.ne twrote in message
                        news:3IidnSr0N4 WfazPVnZ2dnUVZ_ uOdnZ2d@comcast .com...
                        On 2008-08-22, Justin <None@None.comw rote:
                        >Ok, after getting beat in the head with "class" by 4 people I converted
                        >it
                        >to a class :)
                        >>
                        >I tried to convert my arraylist to a list and that was just an ugly mess.
                        >Here's the first two errors I received:
                        >'TabsArray' cannot expose type 'TabClass' outside the project through
                        >class
                        >'frmMain'
                        >Too few type arguments to 'System.Collect ions.Generic.Li st(Of T)'
                        >>
                        >But enough of that. I want to wrap my head around iComparable first.
                        >I'll
                        >deal with that later.
                        >>
                        >I implemented iComparable and now when I try to .sort my arraylist I get:
                        >>
                        >"Failed to compare two elements in the array."
                        >>
                        >Again, thanks for the help everyone!
                        >
                        Using a list should be simply:
                        >
                        Dim myList As New List(Of TabClass)()
                        >
                        from there, it is very much like an ArrayList - only better and faster :)
                        >
                        --
                        Tom Shelton

                        Comment

                        • Justin

                          #13
                          Re: Help with sorting an Arraylist that contains Structures

                          Point well taken. While my data doesn't change so this wont hurt me here, I
                          never knew this about structures and I'll be sure to just use classes from
                          now on.

                          Thanks!


                          "Tom Shelton" <tom_shelton@co mcastXXXXXXX.ne twrote in message
                          news:cM-dnaPkBv8xQTPVnZ 2dnUVZ_vadnZ2d@ comcast.com...
                          On 2008-08-21, Justin <None@None.comw rote:
                          >Ok, I give up. I can't seem to construct a decent (productive) way of
                          >sorting my arraylist.
                          >>
                          >I have a structure of two elements:
                          >>
                          >Structure TabStructure
                          > Dim TabName As String
                          > Dim FullFilePath As String
                          >End Structure
                          >>
                          >Then for example I:
                          >>
                          >Dim tab as New TabStructure
                          >>
                          >some code
                          >>
                          >Then I add "tab" to an arraylist.
                          >>
                          >At some point I loop through my arraylist, grab data and create tabs from
                          >it. Of course the resulting list of tabs are out of sequence.
                          >>
                          >Can someone help me to sort my array list alphabetically by
                          >TabStructure.T abName?
                          >>
                          >I would greatly appreciate it.
                          >>
                          >
                          Others have point out to you the List(Of T) - so I won't rehash that :)
                          But,
                          I'm going to talk about your structure. The fact is, there is almost
                          never
                          any reason to use a structure except for interop scenarios or where you
                          want
                          to preserve value symantics... TabStructure should be a class. This will
                          become apparent as soon as you start trying to modify individual members
                          of
                          your list:
                          >
                          Private Structure TStruct
                          Public s1 As String
                          Public s2 As String
                          >
                          Public Sub New(ByVal s1 As String, ByVal s2 As String)
                          Me.s1 = s1
                          Me.s2 = s2
                          End Sub
                          End Structure
                          >
                          Sub Main()
                          Dim l As New List(Of TStruct)
                          l.Add(New TStruct("one", "one"))
                          l.Add(New TStruct("two", "one"))
                          l.Add(New TStruct("three" , "one"))
                          l.Add(New TStruct("four", "one"))
                          >
                          For Each i As TStruct In l
                          i.s2 = "changing.. ."
                          Next
                          >
                          For Each i As TStruct In l
                          Console.WriteLi ne("s1={0}, s2={1}", i.s1, i.s2)
                          Next
                          End Sub
                          >
                          You might be suprised that the values of s2 changend in the first loop,
                          don't
                          actually change anything. That's because TStruct is a value type, and any
                          time you assign or return it you get a copy and not the original. If I
                          change
                          TStruct to a class:
                          >
                          Private Class TStruct
                          Public s1 As String
                          Public s2 As String
                          >
                          Public Sub New(ByVal s1 As String, ByVal s2 As String)
                          Me.s1 = s1
                          Me.s2 = s2
                          End Sub
                          End Class
                          >
                          Then every thing works as expected.
                          >
                          So, again unless your doing interop or really want value symantics, there
                          is
                          not much gained by using a struct over a class.
                          >
                          --
                          Tom Shelton

                          Comment

                          Working...