ArrayList, Array or something else

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

    #1

    ArrayList, Array or something else

    Hi,

    I would like to have something like an array (something like a
    collection) in order to store the following things :

    company name1, new1, old1, in1, out1
    company name2, new2, old2, in2, out2
    ....

    but i should be able to dynamically redimension it.
    all data will be strings only.

    I was thinking to use an ArrayList, but how to do a 2-dim Arraylist of
    string ?

    thanks a lot,
    Maileen
  • Herfried K. Wagner [MVP]

    #2
    Re: ArrayList, Array or something else

    "Maileen" <noemail@nospam .com> schrieb:[color=blue]
    > I would like to have something like an array (something like a
    > collection) in order to store the following things :
    >
    > company name1, new1, old1, in1, out1
    > company name2, new2, old2, in2, out2
    > ...
    >
    > but i should be able to dynamically redimension it.
    > all data will be strings only.[/color]


    \\\
    Public Class Company
    Public Name As String
    Public [New] As String
    Public Old As String
    Public ...
    End Class
    ..
    ..
    ..
    Dim a As New ArrayList()
    Dim c As New Company()
    ....
    a.Add(c)
    ....
    ///

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Maileen

      #3
      Re: ArrayList, Array or something else

      Thanks a lot.
      I was thinking about such thing, but i was not sure that it could be
      possible.
      once again thanks.

      Maileen

      Herfried K. Wagner [MVP] wrote:[color=blue]
      > "Maileen" <noemail@nospam .com> schrieb:
      >[color=green]
      >> I would like to have something like an array (something like a
      >> collection) in order to store the following things :
      >>
      >> company name1, new1, old1, in1, out1
      >> company name2, new2, old2, in2, out2
      >> ...
      >>
      >> but i should be able to dynamically redimension it.
      >> all data will be strings only.[/color]
      >
      >
      >
      > \\\
      > Public Class Company
      > Public Name As String
      > Public [New] As String
      > Public Old As String
      > Public ...
      > End Class
      > .
      > .
      > .
      > Dim a As New ArrayList()
      > Dim c As New Company()
      > ...
      > a.Add(c)
      > ...
      > ///
      >[/color]

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: ArrayList, Array or something else

        Maileen,

        What you describe is a DataTable.

        Or a class build with collectionbase however in my opinion that needs more
        work and endless changing to get the possibilities from a DataTable, when
        more functionality is wanted.

        I hope this helps,

        Cor


        Comment

        • m.posseth

          #5
          Re: ArrayList, Array or something else


          Herfried ,,,

          just curious why you choose this aproach

          Wouldn`t it be better to use a structure in this situation ( as it is more
          lightweight ) ?

          regards

          Michel Posseth


          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
          news:OTsgpzEoFH A.632@tk2msftng p13.phx.gbl...[color=blue]
          > "Maileen" <noemail@nospam .com> schrieb:[color=green]
          >> I would like to have something like an array (something like a
          >> collection) in order to store the following things :
          >>
          >> company name1, new1, old1, in1, out1
          >> company name2, new2, old2, in2, out2
          >> ...
          >>
          >> but i should be able to dynamically redimension it.
          >> all data will be strings only.[/color]
          >
          >
          > \\\
          > Public Class Company
          > Public Name As String
          > Public [New] As String
          > Public Old As String
          > Public ...
          > End Class
          > .
          > .
          > .
          > Dim a As New ArrayList()
          > Dim c As New Company()
          > ...
          > a.Add(c)
          > ...
          > ///
          >
          > --
          > M S Herfried K. Wagner
          > M V P <URL:http://dotnet.mvps.org/>
          > V B <URL:http://classicvb.org/petition/>[/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: ArrayList, Array or something else

            "m.posseth" <michelp@nohaus ystems.nl> schrieb:[color=blue]
            > just curious why you choose this aproach
            >
            > Wouldn`t it be better to use a structure in this situation ( as it is
            > more lightweight ) ?[/color]

            Well, that's hard to decide without further information. Structures
            generally should be small (<= 16 bytes), otherwise classes are preferrable.
            BTW: I forgot to mention that it might be better to use properties instead
            of the public variables.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            • _AnonCoward

              #7
              Re: ArrayList, Array or something else


              "m.posseth" <michelp@nohaus ystems.nl> wrote in message
              news:OySK4mLoFH A.2916@TK2MSFTN GP14.phx.gbl...
              :
              : Herfried ,,,
              :
              : just curious why you choose this aproach
              :
              : Wouldn`t it be better to use a structure in this situation
              : ( as it is more lightweight ) ?
              :
              : regards
              :
              : Michel Posseth


              Structures can have unexpected behavior in these sort of applications.
              Consider the following:


              -----------------------------
              Public Class Cls
              Public Parm As String
              End Class

              Public Class [class]
              Public Shared Sub Main
              Dim A As New ArrayList
              Dim c As New Cls

              c.Parm = "One"
              A.Add(c)
              Console.WriteLi ne(A.Item(0).Pa rm)

              A.Item(0).Parm = "Two"
              Console.WriteLi ne(A.Item(0).Pa rm)
              End Sub
              End Class
              -----------------------------

              The output from this is:

              One
              Two

              However, change the class to a structure and then try:


              -----------------------------
              Public Structure Cls
              Public Parm As String
              End Structure

              Public Class [class]
              Public Shared Sub Main
              Dim A As New ArrayList
              Dim c As New Cls


              c.Parm = "One"
              A.Add(c)
              Console.WriteLi ne(A.Item(0).Pa rm)

              'here is the offending line
              A.Item(0).Parm = "Two"


              Console.WriteLi ne(A.Item(0).Pa rm)
              End Sub
              End Class
              -----------------------------


              This time, you get a runtime error.

              Latebound assignment to a field of value type 'Cls' is
              not valid when 'Cls' is the result of a latebound expression.


              As I understand it, this is a limitation of the framework, not VB.Net,
              because C# will generate a similar error.


              In addition, structures are value types and copy assignments may not
              behave as expected.

              -----------------------------
              Public Structure Cls
              Public Parm As String
              End Structure

              Public Class [class]
              Public Shared Sub Main
              Dim A As New ArrayList
              Dim c As New Cls
              Dim c2 As Cls

              c.Parm = "One"
              A.Add(c)
              Console.WriteLi ne(A.Item(0).Pa rm)

              'we're grabbing the VALUE type at item 0
              'and copying to the c2 variable
              c2 = A.Item(0)

              'the change to the copy is not reflected
              'in the original item
              c2.Parm = "Two"
              Console.WriteLi ne(A.Item(0).Pa rm)
              End Sub
              End Class

              -----------------------------

              The output from this is:

              One
              One


              Change this to a class again and you'll get different results:


              -----------------------------
              Public Structure Cls
              Public Parm As String
              End Structure

              Public Class [class]
              Public Shared Sub Main
              Dim A As New ArrayList
              Dim c As New Cls
              Dim c2 As Cls


              'we're grabbing the REFERENCE type at item 0
              'and copying the reference to the c2 variable
              c.Parm = "One"
              A.Add(c)
              Console.WriteLi ne(A.Item(0).Pa rm)

              'the change to the copy IS reflected
              'in the original item
              c2 = A.Item(0)
              c2.Parm = "Two"
              Console.WriteLi ne(A.Item(0).Pa rm)
              End Sub
              End Class

              -----------------------------


              The output from this is:

              One
              Two


              The recommendation I've seen regarding classes vs. structures is you
              should normally stick with classes unless you have a specific need to do
              otherwise.


              <snip>


              Ralf
              --
              ----------------------------------------------------------
              * ^~^ ^~^ *
              * _ {~ ~} {~ ~} _ *
              * /_``>*< >*<''_\ *
              * (\--_)++) (++(_--/) *
              ----------------------------------------------------------
              There are no advanced students in Aikido - there are only
              competent beginners. There are no advanced techniques -
              only the correct application of basic principles.


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: ArrayList, Array or something else

                "_AnonCowar d" <abc@xyz.com> schrieb:[color=blue]
                > -----------------------------
                > Public Structure Cls
                > Public Parm As String
                > End Structure
                >
                > Public Class [class]
                > Public Shared Sub Main
                > Dim A As New ArrayList
                > Dim c As New Cls
                >
                >
                > c.Parm = "One"
                > A.Add(c)
                > Console.WriteLi ne(A.Item(0).Pa rm)
                >
                > 'here is the offending line
                > A.Item(0).Parm = "Two"
                >
                >
                > Console.WriteLi ne(A.Item(0).Pa rm)
                > End Sub
                > End Class
                > -----------------------------
                >
                >
                > This time, you get a runtime error.
                >
                > Latebound assignment to a field of value type 'Cls' is
                > not valid when 'Cls' is the result of a latebound expression.
                >
                >
                > As I understand it, this is a limitation of the framework, not VB.Net,
                > because C# will generate a similar error.[/color]


                One thing you can do is using 'DirectCast':

                \\\
                DirectCast(A.It em(0), Cls).Parm = "Two"
                ///

                However, note that this won't work with structures, because a copy of the
                actual object will be returned, and changing a property of the copy doesn't
                make any sense.

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://classicvb.org/petition/>

                Comment

                • _AnonCoward

                  #9
                  Re: ArrayList, Array or something else


                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                  news:%23h9gYwRo FHA.2916@TK2MSF TNGP14.phx.gbl. ..
                  :
                  : "_AnonCowar d" <abc@xyz.com> schrieb:
                  : >
                  : > -----------------------------
                  : > Public Structure Cls
                  : > Public Parm As String
                  : > End Structure
                  : >
                  : > Public Class [class]
                  : > Public Shared Sub Main
                  : > Dim A As New ArrayList
                  : > Dim c As New Cls
                  : >
                  : >
                  : > c.Parm = "One"
                  : > A.Add(c)
                  : > Console.WriteLi ne(A.Item(0).Pa rm)
                  : >
                  : > 'here is the offending line
                  : > A.Item(0).Parm = "Two"
                  : >
                  : >
                  : > Console.WriteLi ne(A.Item(0).Pa rm)
                  : > End Sub
                  : > End Class
                  : > -----------------------------
                  : >
                  : >
                  : > This time, you get a runtime error.
                  : >
                  : > Latebound assignment to a field of value type 'Cls' is
                  : > not valid when 'Cls' is the result of a latebound expression.
                  : >
                  : >
                  : > As I understand it, this is a limitation of the framework, not
                  : > VB.Net, because C# will generate a similar error.
                  :
                  :
                  : One thing you can do is using 'DirectCast':
                  :
                  : \\\
                  : DirectCast(A.It em(0), Cls).Parm = "Two"
                  : ///
                  :
                  : However, note that this won't work with structures, because a copy of
                  : the actual object will be returned, and changing a property of the
                  : copy doesn't make any sense.


                  Agreed. In this case, it gets rid of the error message but you still
                  can't achieve the desired effect - you need a class for this.


                  The problems with structures go even deeper. Consider this variation:

                  ---------------------------------
                  Public Structure Struct
                  Public n As Integer
                  End Structure

                  Public Class Cls
                  Private s As New Struct

                  Public Property Str() As Struct
                  Get
                  Return s
                  End Get
                  Set
                  s = Value
                  End Set
                  End Property
                  End Class


                  Public Class [class]
                  Public Shared Sub Main
                  Dim c As New Cls
                  c.Str.n = 1
                  End Sub
                  End Class
                  ---------------------------------


                  This generates the compiler following error:


                  Expression is a value and therefore cannot be
                  the target of an assignment.

                  c.Str.n = 1
                  ~~~~~~~

                  You cannot access the value type "Struct" directly from the parent Cls
                  object. However, if Struct is declared as a class, this compiles just
                  fine.


                  This is easy enough to work around, of course - just make the following
                  modification (however, this does serve to illustrate how quirky
                  structures are):


                  ---------------------------------
                  Public Class [class]
                  Public Shared Sub Main
                  Dim c As New Cls
                  Dim s As Struct = c.Str
                  s.n = 1
                  End Sub
                  End Class
                  ---------------------------------


                  As long as we're on this subject, and going back to the original
                  question of why a class instead of a structure, there is another
                  consideration as well:


                  <modification to original code example>

                  \\\
                  Public Structure Company
                  Public Name As String
                  Public [New] As String
                  Public Old As String
                  Public ...
                  End Structure
                  ..
                  ..
                  ..
                  Dim a As New ArrayList()
                  Dim c As New Company()
                  ....
                  a.Add(c)
                  ///


                  As I understand it, since the ArrayList.Add function only accepts
                  objects, inserting a value type (i.e.: the Structure 'Company' above)
                  will involve boxing and unboxing. There are expensive operations that
                  would offset any gains realized by using a structure in this case.


                  Ralf
                  --
                  ----------------------------------------------------------
                  * ^~^ ^~^ *
                  * _ {~ ~} {~ ~} _ *
                  * /_``>*< >*<''_\ *
                  * (\--_)++) (++(_--/) *
                  ----------------------------------------------------------
                  There are no advanced students in Aikido - there are only
                  competent beginners. There are no advanced techniques -
                  only the correct application of basic principles.


                  Comment

                  Working...