{"id":4960,"date":"2014-03-30T17:12:29","date_gmt":"2014-03-30T17:12:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/creating-arrays-and-resizing-them-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:12:29","modified_gmt":"2014-03-30T17:12:29","slug":"creating-arrays-and-resizing-them-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/creating-arrays-and-resizing-them-collection-of-common-programming-errors\/","title":{"rendered":"Creating arrays and resizing them-Collection of common programming errors"},"content":{"rendered":"<p>ReDim Preserve will do it, and if the array were declared at the module level, any code referencing it will not lose the reference. I do believe this is specific to vb, however, and there is also a performance penalty, in that this, too, is creating a copy of the array.<\/p>\n<p>I haven&#8217;t checked, but I suspect the method user274204 describes above is probably the CLR-compliant way to do this . .<\/p>\n<p>Public Class Form1<\/p>\n<pre><code>Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\n\n    'Initialize your array:\n    Dim Integers(20) As Integer\n\n    'Output to the console, and you will see 20 elements of value 0\n    Me.OutputArrayValues(Integers)\n\n    'Iterate through each element and assign an integer Value:\n    For i = 0 To UBound(Integers)\n        Integers(i) = i\n    Next\n\n    'Output to console, and you will have values from 0 to 20:\n    Me.OutputArrayValues(Integers)\n\n    'Use Redim Preserve to expand the array to 30 elements:\n    ReDim Preserve Integers(30)\n\n    'output will show the same 0-20 values in elements 0 thru 20, and then 10 0 value elements:\n    Me.OutputArrayValues(Integers)\n\n    'Redim Preserve again to reduce the number of elements without data loss:\n    ReDim Preserve Integers(15)\n\n    'Same as above, but elements 16 thru 30 are gone:\n    Me.OutputArrayValues(Integers)\n\n    'This will re-initialize the array with only 5 elements, set to 0:\n    ReDim Integers(5)\n    Me.OutputArrayValues(Integers)\n\n\nEnd Sub\n\nPrivate Sub OutputArrayValues(ByVal SomeArray As Array)\n    For Each i As Object In SomeArray\n        Console.WriteLine(i)\n    Next\nEnd Sub\n<\/code><\/pre>\n<p>End Class<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ReDim Preserve will do it, and if the array were declared at the module level, any code referencing it will not lose the reference. I do believe this is specific to vb, however, and there is also a performance penalty, in that this, too, is creating a copy of the array. I haven&#8217;t checked, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4960","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4960"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4960\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}