Adding and Removing from a variable Array?

Sorry for the beginner question but I can't seem to figure out how to add or remove values from an array of variable size. For example, this works without problem:

Public Class Form1

Public config(2) As String


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
config(0) = "test"

end sub

End Class

This however doesn't... I've tried various variations on this and I can't seem to figure it out.

Public Class Form1

Public config() As String


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
config(0) = "test"

end sub

End Class


How do I add or remove elements from an array in this very basic situation




Answer this question

Adding and Removing from a variable Array?

  • drewex

    Thanks, ReDim was the command I needed. :)

  • kaborka

    To allocate 3 elements in config() write this:
    Redim config(2)
    config(0) = "test"


  • OzerK

    OK, the first works as youve dimensioned an provided the array with a dimension which will instantiate the array object.

    The second will fail as youve established a reference variable to an array but havent given it any dimensions so it isnt yet instantiated.

    Using the second will give you and "Object reference not set to an instance of an object." exception just like if you do a

    dim x as Class1 and then try and use x.   You need to do a dim x as New Class1 to instantiate a new instance of Class1 as an Object.

    WHy would you want the second.   You may have an array you want as class level scoped but want to dimension it in a method.   So you need to specify the array as a class level variable and provide the dimension in the method.

    The following code example will work if you press button1 before button2 as button1 as button1 adds dimension to the array and hence instantiates it.   If you then press button2 it will display the contents.

    If you restart the program and press button2 first it will fail because the array has not yet been dimensioned, press button1 and button2 again and it will display the contents.

    Public Class Form1
        Public s() As String

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ReDim s(2)
            s(0) = "dddd"
            MsgBox(s(0))
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            MsgBox(s(0))
        End Sub
    End Class

     

    Redim allows you to redimension an array after its been created with a few limitations

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/script56/html/5c12ce79-6616-4144-b3b6-4cffe3884dfd.asp

     


  • joelw7

    In addition to what Spotty said, if you want dynamically sized arrays, look into the StringCollection, ArrayList, and other objects in the System.Collections.Specialized namespace. Also, in 2005, consider the new generic lists.

    Jim Wooley
    http://devauthority.com/blogs/jwooley/default.aspx



  • Adding and Removing from a variable Array?