An odd array feature in vb.net

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

    #1

    An odd array feature in vb.net

    I have two independent arrays and mysteriously they occupy the same
    space. Is this an error or a feature?

    Dim tokens$() '(input list of msn's and
    constants)
    Dim values() As Object '(tokens with msn's replaced by
    values)

    temp="A = 2 * B"

    '---parse the temp expression into tokens
    tokens = Split(temp, " ")

    '---setup a similar sized values array
    ReDim values(UBound(t okens))
    values=tokens

    and under vb.net changing tokens(4) will now change values(4)!!.

    I expected that making values = tokens would give me an array "copy by
    element". In fact it made the two occupy the same array space. Is
    this a bug or a language feature??



  • Lloyd Sheen

    #2
    Re: An odd array feature in vb.net


    "barcrofter " <jd.prsn@gmail. comwrote in message
    news:5d970428-59ef-43c1-8ff2-4966d5fc7c09@a3 g2000prm.google groups.com...
    >I have two independent arrays and mysteriously they occupy the same
    space. Is this an error or a feature?
    >
    Dim tokens$() '(input list of msn's and
    constants)
    Dim values() As Object '(tokens with msn's replaced by
    values)
    >
    temp="A = 2 * B"
    >
    '---parse the temp expression into tokens
    tokens = Split(temp, " ")
    >
    '---setup a similar sized values array
    ReDim values(UBound(t okens))
    values=tokens
    >
    and under vb.net changing tokens(4) will now change values(4)!!.
    >
    I expected that making values = tokens would give me an array "copy by
    element". In fact it made the two occupy the same array space. Is
    this a bug or a language feature??
    >
    >
    >
    First a couple of comments:

    Since you are using Dot.Net I would suggest using the new syntax so:
    dim Tokens() as string

    Ensure that you have options strict and explicit set to on. This will cause
    you a bit of hassle at first but will pay big dividends when debugging your
    app. You will then have to declare the temp variable.

    As for you question you are setting a reference to an array to another array
    so they will both point to the same array. What you might want to do is use
    one of the Copy methods to bring the items from one array to the other.

    Also if you are not looking to export this array to unmanaged code I would
    suggest looking into another collection type for example a List(of T). This
    will allow you to manipulate the List without having to redim all the time.

    LS


    Comment

    • Tom Shelton

      #3
      Re: An odd array feature in vb.net

      On 2008-11-18, barcrofter <jd.prsn@gmail. comwrote:
      I have two independent arrays and mysteriously they occupy the same
      space. Is this an error or a feature?
      >
      Dim tokens$() '(input list of msn's and
      constants)
      Dim values() As Object '(tokens with msn's replaced by
      values)
      >
      temp="A = 2 * B"
      >
      '---parse the temp expression into tokens
      tokens = Split(temp, " ")
      >
      '---setup a similar sized values array
      ReDim values(UBound(t okens))
      values=tokens
      >
      and under vb.net changing tokens(4) will now change values(4)!!.
      >
      I expected that making values = tokens would give me an array "copy by
      element". In fact it made the two occupy the same array space. Is
      this a bug or a language feature??
      >
      >
      >
      Arrays are reference types - so nothing is copied, except the reference. If
      you want a copy, then you need to do something like this:

      redim values(ubound(t okens))
      tokens.copyto(v alue, 0)

      Though, be aware that if the array contains reference types, then all that is
      copied in each element is the reference... so changing properties on an
      object in one array will be reflected in the other.

      --
      Tom Shelton

      Comment

      • Armin Zingler

        #4
        Re: An odd array feature in vb.net

        "barcrofter " <jd.prsn@gmail. comschrieb
        ReDim values(UBound(t okens))
        values=tokens
        >
        and under vb.net changing tokens(4) will now change values(4)!!.
        >
        I expected that making values = tokens would give me an array "copy by
        element". In fact it made the two occupy the same array space. Is
        this a bug or a language feature??

        Lookup "reference types" in the documentation (in opposite to value types).
        An array is a reference type. You've just copied a reference. 'values'
        references the same array as 'tokens'. You can ommit the "Redim" I quoted
        because it's overwritten in the next line "values=tokens" .


        Armin

        Comment

        • Patrice

          #5
          Re: An odd array feature in vb.net

          This is by design. See :
          http://msdn.microsoft.com/en-us/libr...hs(VS.80).aspx
          (Value Types and Reference Types)

          As "A reference type contains a pointer to another memory location that
          holds the data" and arrays are reference type it means that values=tokens
          make the "values" variable to point to the same memory location than
          "tokens" (and this is true for all objects).

          See :
          http://msdn.microsoft.com/en-us/libr...rray.copy.aspx for how to
          copy the array content...

          --
          Patrice

          "barcrofter " <jd.prsn@gmail. coma écrit dans le message de groupe de
          discussion :
          5d970428-59ef-43c1-8ff2-4966d5fc7c09...le groups.com...
          I have two independent arrays and mysteriously they occupy the same
          space. Is this an error or a feature?
          >
          Dim tokens$() '(input list of msn's and
          constants)
          Dim values() As Object '(tokens with msn's replaced by
          values)
          >
          temp="A = 2 * B"
          >
          '---parse the temp expression into tokens
          tokens = Split(temp, " ")
          >
          '---setup a similar sized values array
          ReDim values(UBound(t okens))
          values=tokens
          >
          and under vb.net changing tokens(4) will now change values(4)!!.
          >
          I expected that making values = tokens would give me an array "copy by
          element". In fact it made the two occupy the same array space. Is
          this a bug or a language feature??
          >
          >
          >

          Comment

          • barcrofter

            #6
            Re: An odd array feature in vb.net

            Wow! Humbling.

            Comment

            Working...