An Array question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RWR3YXJk?=

    An Array question

    Hi everybody,
    To me the following code shouldn't work but it does !
    Imports system.String
    Dim x As String="This,is ,a,test"
    Dim y(1) As String
    y=x.Split(",")
    TextBox1.text=y (3)
    why "Y" which is an array of lenght 2 accepts index 3 which is larger than
    its lenght?
    Any thoughts?
    --
    Best regards,
    Edward
  • Lloyd Sheen

    #2
    Re: An Array question


    "Edward" <Edward@discuss ions.microsoft. comwrote in message
    news:EDEC95C8-DEA3-40D0-8A4B-544B681AAD26@mi crosoft.com...
    Hi everybody,
    To me the following code shouldn't work but it does !
    Imports system.String
    Dim x As String="This,is ,a,test"
    Dim y(1) As String
    y=x.Split(",")
    TextBox1.text=y (3)
    why "Y" which is an array of lenght 2 accepts index 3 which is larger
    than
    its lenght?
    Any thoughts?
    --
    Best regards,
    Edward
    The statement:
    y=x.Split(",")

    creates a new array and assigns it to y. The first assignment allows for
    items (0) and (1). The reassignment (the split statement) just returns an
    array the size of which depends on the source string and the split string.

    LS

    Comment

    • =?Utf-8?B?RWR3YXJk?=

      #3
      Re: An Array question

      yes but I was wondering about the logic behind this kind of behavior , when
      we define a fixed size for an array isn't it suppose to keep its fixed size ?
      In fact we can assign a larger array to a smaller array and VB doesn't
      complian and resizes the smaller array!
      --
      Best regards,
      Edward


      "Lloyd Sheen" wrote:
      >
      "Edward" <Edward@discuss ions.microsoft. comwrote in message
      news:EDEC95C8-DEA3-40D0-8A4B-544B681AAD26@mi crosoft.com...
      Hi everybody,
      To me the following code shouldn't work but it does !
      Imports system.String
      Dim x As String="This,is ,a,test"
      Dim y(1) As String
      y=x.Split(",")
      TextBox1.text=y (3)
      why "Y" which is an array of lenght 2 accepts index 3 which is larger
      than
      its lenght?
      Any thoughts?
      --
      Best regards,
      Edward
      >
      The statement:
      y=x.Split(",")
      >
      creates a new array and assigns it to y. The first assignment allows for
      items (0) and (1). The reassignment (the split statement) just returns an
      array the size of which depends on the source string and the split string.
      >
      LS
      >
      >

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: An Array question

        "Edward" <Edward@discuss ions.microsoft. comschrieb:
        To me the following code shouldn't work but it does !
        Imports system.String
        Dim x As String="This,is ,a,test"
        Dim y(1) As String
        ='Dim y() As String'.
        y=x.Split(",")
        TextBox1.text=y (3)
        why "Y" which is an array of lenght 2 accepts index 3 which is larger
        than
        its lenght?
        'Y' references an array of length 4 with indices 0, ..., 3 after the
        assignment of 'Split''s return value. 'Y(3)' contains "test".

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

        Comment

        • Lloyd Sheen

          #5
          Re: An Array question


          "Edward" <Edward@discuss ions.microsoft. comwrote in message
          news:F8C9DCBA-D097-48BA-917D-DAD0B29159AA@mi crosoft.com...
          yes but I was wondering about the logic behind this kind of behavior ,
          when
          we define a fixed size for an array isn't it suppose to keep its fixed
          size ?
          In fact we can assign a larger array to a smaller array and VB doesn't
          complian and resizes the smaller array!
          --
          Best regards,
          Edward
          >
          >
          "Lloyd Sheen" wrote:
          >
          >>
          >"Edward" <Edward@discuss ions.microsoft. comwrote in message
          >news:EDEC95C 8-DEA3-40D0-8A4B-544B681AAD26@mi crosoft.com...
          Hi everybody,
          To me the following code shouldn't work but it does !
          Imports system.String
          Dim x As String="This,is ,a,test"
          Dim y(1) As String
          y=x.Split(",")
          TextBox1.text=y (3)
          why "Y" which is an array of lenght 2 accepts index 3 which is larger
          than
          its lenght?
          Any thoughts?
          --
          Best regards,
          Edward
          >>
          >The statement:
          >y=x.Split(", ")
          >>
          >creates a new array and assigns it to y. The first assignment allows for
          >items (0) and (1). The reassignment (the split statement) just returns
          >an
          >array the size of which depends on the source string and the split
          >string.
          >>
          >LS
          >>
          >>
          It really is no different than any other variable assignment. At one moment
          you have an array with capacity of 2 (0) and (1). When you use the x.split
          you assign a new array. The old one is put in for garbage collection unless
          there is another reference to it.

          Same as if I assign a new string to a string varaible. I think you are
          taking the first assignment as a "set in concrete" statement where it is
          not. Until another assignment happens (which is the split) it has one array
          and then after the assignment (split) you now have a new one.

          LS

          Comment

          • ShaneO

            #6
            Re: An Array question

            Edward wrote:
            yes but I was wondering about the logic behind this kind of behavior , when
            we define a fixed size for an array isn't it suppose to keep its fixed size ?
            In fact we can assign a larger array to a smaller array and VB doesn't
            complian and resizes the smaller array!
            You need to understand that "Split" basically does a REDIM on your
            variable, however, if you really must limit the size of the Array, try
            the following -

            Dim x As String = "This,is,a,test "
            Dim y() As String = Split(x, ",", 2)

            You'll find that "y" is now limited to just two elements. (0 and 1)

            I hope this helps.

            ShaneO

            There are 10 kinds of people - Those who understand Binary and those who
            don't.

            Comment

            • Phill W.

              #7
              Re: An Array question

              Edward wrote:
              I was wondering about the logic behind this kind of behavior , when
              we define a fixed size for an array isn't it suppose to keep its fixed size ?
              Nope.
              In fact we can assign a larger array to a smaller array and VB doesn't
              complain and resizes the smaller array!
              It /used/ to be that the equivalent, VB "Proper" code ...

              Dim y(1) As String
              y = Split(x, ",")

              .... wouldn't even /compile/ precisely because of this discontinuity.

              In our Brave New World.Net, however, Split() creates a whole /new/ array
              and dumps a reference to it back into the variable "y", junking whatever
              may or may not have been there before; there's no relationship at all
              between the previous array and the one that you're now assigning to it.

              HTH,
              Phill W.

              Comment

              • Chris Dunaway

                #8
                Re: An Array question

                On Jun 2, 6:09 pm, ShaneO <spc...@optusne t.com.auwrote:
                Edward wrote:
                yes but I was wondering about the logic behind this kind of behavior , when
                we define a fixed size for an array isn't it suppose to keep its fixed size ?
                In fact we can assign a larger array to a smaller array and VB doesn't
                complian and resizes the smaller array!
                >
                You need to understand that "Split" basically does a REDIM on your
                variable, however, if you really must limit the size of the Array, try
                the following -
                This is not accurate. The Split function does not ReDim the existing
                array. Arrays are reference types. As pointed out earlier, Split
                creates a *new* array and then assigns a reference to it to variable
                y, discarding the old reference. There is no ReDim involved, you can
                use Reflector to verify that.

                Chris

                Comment

                • Cor Ligthert[MVP]

                  #9
                  Re: An Array question

                  Chris,

                  I wished I could tell you in Dutch


                  But what is in a word, there is done again a Dimension of an Array. So to
                  use the sentence ReDim is in my idea not that wrong. That ReDim keyword
                  restores the values of an old array is in my idea not the right use of the
                  meaning of the word in English.

                  However who am I to state that.

                  :-)

                  Cor

                  "Chris Dunaway" <dunawayc@gmail .comschreef in bericht
                  news:d0be264a-f86c-4dc0-a70a-b778d92e2203@b1 g2000hsg.google groups.com...
                  On Jun 2, 6:09 pm, ShaneO <spc...@optusne t.com.auwrote:
                  >Edward wrote:
                  yes but I was wondering about the logic behind this kind of behavior ,
                  when
                  we define a fixed size for an array isn't it suppose to keep its fixed
                  size ?
                  In fact we can assign a larger array to a smaller array and VB doesn't
                  complian and resizes the smaller array!
                  >>
                  >You need to understand that "Split" basically does a REDIM on your
                  >variable, however, if you really must limit the size of the Array, try
                  >the following -
                  >
                  This is not accurate. The Split function does not ReDim the existing
                  array. Arrays are reference types. As pointed out earlier, Split
                  creates a *new* array and then assigns a reference to it to variable
                  y, discarding the old reference. There is no ReDim involved, you can
                  use Reflector to verify that.
                  >
                  Chris

                  Comment

                  • ShaneO

                    #10
                    Re: An Array question

                    Chris Dunaway wrote:
                    On Jun 2, 6:09 pm, ShaneO <spc...@optusne t.com.auwrote:
                    >Edward wrote:
                    >>yes but I was wondering about the logic behind this kind of behavior , when
                    >>we define a fixed size for an array isn't it suppose to keep its fixed size ?
                    >>In fact we can assign a larger array to a smaller array and VB doesn't
                    >>complian and resizes the smaller array!
                    >You need to understand that "Split" basically does a REDIM on your
                    >variable, however, if you really must limit the size of the Array, try
                    >the following -
                    >
                    This is not accurate. The Split function does not ReDim the existing
                    array. Arrays are reference types. As pointed out earlier, Split
                    creates a *new* array and then assigns a reference to it to variable
                    y, discarding the old reference. There is no ReDim involved, you can
                    use Reflector to verify that.
                    >
                    Chris
                    Thank-you Chris, you are technically correct, however as I wrote in my
                    post it "basically does a REDIM". At the end of the day the original
                    variables reference is replaced with a reference to a new array created
                    by the Split function, but so what, for the sake of simplicity in
                    replying to the OP question, it "basically does a REDIM". :-)


                    ShaneO

                    There are 10 kinds of people - Those who understand Binary and those who
                    don't.

                    Comment

                    • =?Utf-8?B?RWR3YXJk?=

                      #11
                      Re: An Array question

                      I guess I was not thinking 100% OOP ,although I knew this concept from Java
                      still I was thinking in terms of VBA but I'm back to the track now.
                      --
                      Best regards,
                      Edward


                      "Herfried K. Wagner [MVP]" wrote:
                      "Edward" <Edward@discuss ions.microsoft. comschrieb:
                      To me the following code shouldn't work but it does !
                      Imports system.String
                      Dim x As String="This,is ,a,test"
                      Dim y(1) As String
                      >
                      ='Dim y() As String'.
                      >
                      y=x.Split(",")
                      TextBox1.text=y (3)
                      why "Y" which is an array of lenght 2 accepts index 3 which is larger
                      than
                      its lenght?
                      >
                      'Y' references an array of length 4 with indices 0, ..., 3 after the
                      assignment of 'Split''s return value. 'Y(3)' contains "test".
                      >
                      --
                      M S Herfried K. Wagner
                      M V P <URL:http://dotnet.mvps.org/>
                      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
                      >
                      >

                      Comment

                      • Chris Dunaway

                        #12
                        Re: An Array question

                        On Jun 3, 11:53 am, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
                        wrote:
                        Chris,
                        >
                        I wished I could tell you in Dutch
                        >
                        But what is in a word, there is done again a Dimension of an Array. So to
                        use the sentence ReDim is in my idea not that wrong. That ReDim keyword
                        restores the values of an old array is in my idea not the right use of the
                        meaning of the word in English.
                        >
                        I understand what you are saying. I just wanted to make sure the OP
                        realized that calling Split creates an entirely new array and then
                        assigns that to the array variable. I wanted to make it clear that
                        the existing array is not reused in some manner.

                        Cheers,

                        Chris

                        Comment

                        • Chris Dunaway

                          #13
                          Re: An Array question

                          On Jun 3, 2:41 pm, ShaneO <spc...@optusne t.com.auwrote:
                          Thank-you Chris, you are technically correct, however as I wrote in my
                          post it "basically does a REDIM". At the end of the day the original
                          variables reference is replaced with a reference to a new array created
                          by the Split function, but so what, for the sake of simplicity in
                          replying to the OP question, it "basically does a REDIM". :-)
                          Yes I understood your meaning and agree. I did a little investigating
                          with Reflector and ReDim without Preserve basically does create a new
                          array. As I told Cor, I wanted the OP to be clear that Split does not
                          somehow modify or use the existing array, but you are right about
                          ReDim.

                          Chris

                          Comment

                          • andrews

                            #14
                            Re: An Array question


                            you could also write y() as string,(normal action), as there are 4
                            strings you have also indexes from 0 to 3

                            Comment

                            • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

                              #15
                              Re: An Array question

                              Edward wrote:
                              I guess I was not thinking 100% OOP ,although I knew this concept from Java
                              still I was thinking in terms of VBA but I'm back to the track now.
                              As VB has retained it's syntax for arrays, but not how they work, it can
                              get a bit confusing. The code doesn't always look like it does what it does.

                              This alternate syntax for declaring an array reference corresponds a bit
                              more to what the code actually does:

                              Dim x As String()

                              "x" is the name of the variable, and "String()" is the type of the variable.

                              --
                              Göran Andersson
                              _____
                              Göran Anderssons privata hemsida.

                              Comment

                              Working...