array variable

How can we create an array variable in vb. The size of the array should be defined only during runtime. Please give the sample code.


Answer this question

array variable

  • GGavars

    You may decide to use and arraylist and simply add items to the list.   This way you dont have to be concerned about Redim

    and use the following to add objects to the arraylist


    Dim strList As New ArrayList

    strList.Add("Item1")
    strList.Add(
    "Item2")


     


  • justme2005

    Try this:

    Console.WriteLine("Enter size of array: ")
    Dim numElements As Integer = Integer.Parse(Console.ReadLine())
    Dim strings(numElements) As String
    For i As Integer = 0 To strings.Length - 1
      strings(i) = "Hello, World"
    Next

    For Each s As String In strings
      Console.WriteLine(s)
    Next

    You can also use ReDim Preserve strings(numElements) to resize an existing array keeping current elements. (You can omit Preserve if you want to throw away existing elements.)

    More information on declaring arrays in VB.NET can be found here:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcn7/html/vacondeclaringarrays.asp



  • array variable