VB.Net Tutorial/Collections/ReDim

Материал из VB Эксперт
Перейти к: навигация, поиск

Dimension the array at runtime

public class Test
   public Shared Sub Main
        Dim aryDynArray() As Integer
        Dim intResult As Integer
        Dim intCounter As Integer
        "Dimension the array at runtime.
        intResult = 12
        ReDim aryDynArray(intResult - 1)
        Console.WriteLine("Array size is: " & aryDynArray.Length)
        "Populate the array.
        For intCounter = 0 To aryDynArray.GetUpperBound(0)
            aryDynArray(intCounter) = intCounter + 100
        Next intCounter

   End Sub
End class
Array size is: 12

Dynamic array

Option Strict On
Public Class DynamicArray
   Public Shared Sub Main()
      Dim index As Integer = 10
      Dim arr(index) As Integer
      arr(index) = CInt(index ^ 2)
      index = 20
      ReDim Preserve arr(index)
      arr(index) = CInt(index ^ 2)
      index = 10
      ReDim Preserve arr(index)
      Console.WriteLine(arr(index))
   End Sub
End Class
100

ReDim an array

Module Module1
    Sub Main()
        Dim Scores() As Integer
        ReDim Scores(1)
        Scores(0) = 25
        Scores(1) = 75
        ReDim Preserve Scores(5)
        Scores(2) = 95
        Scores(3) = 35
        Scores(4) = 65
        Scores(5) = 85
        Console.WriteLine("Scores(3) = " & Scores(3))
    End Sub
End Module
Scores(3) = 35

Redimension the array and preserve its element values

public class Test
   public Shared Sub Main
        Dim aryDynArray() As Integer
        Dim intResult As Integer
        Dim intCounter As Integer
        "Dimension the array at runtime.
        intResult = 12
        ReDim aryDynArray(intResult - 1)
        Console.WriteLine("Array size is: " & aryDynArray.Length)
        "Populate the array.
        For intCounter = 0 To aryDynArray.GetUpperBound(0)
            aryDynArray(intCounter) = intCounter + 100
        Next intCounter
        "Redimension the array and preserve its element values.
        ReDim Preserve aryDynArray(aryDynArray.GetUpperBound(0) + 1)
        Console.WriteLine("Array size is: " & aryDynArray.Length)
        Console.WriteLine("First array value is: " & aryDynArray(0))
        "Erase the array.
        Erase aryDynArray

   End Sub
End class
Array size is: 12
Array size is: 13
First array value is: 100

Redim Preserve array

Option Strict On
Public Module modMain
   Public Sub Main()
      Dim PowerOf2() As Integer = {0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}
      Redim Preserve PowerOf2(PowerOf2.GetUpperBound(0)\2)
      For Each int As Integer In PowerOf2
         Console.Write("--{0}--", int)
      Next
   End Sub
End Module
--0----1----2----4----8----16--"

ReDim string array length

Public Class Tester
    Public Shared Sub Main
    
        Dim DaysOfTheWeek() As String
    
        ReDim DaysOfTheWeek(7)
    
        DaysOfTheWeek(0) = "Monday"
        DaysOfTheWeek(1) = "Tuesday"
        DaysOfTheWeek(2) = "Wednesday"
        DaysOfTheWeek(3) = "Thursday"
        DaysOfTheWeek(4) = "Friday"
        DaysOfTheWeek(5) = "Saturday"
        DaysOfTheWeek(6) = "Sunday"
    
        Console.WriteLine(DaysOfTheWeek( 1))
    End Sub
End Class
Tuesday

Use ReDim Preserve to add elements to an array

Public Class Tester
    Public Shared Sub Main
        Dim growingArray() As String = Nothing
        " ----- Add elements to the array.
        For counter As Integer = 0 To 2
            ReDim Preserve growingArray(counter)
            growingArray(counter) = (counter + 1).ToString
        Next counter
        " ----- Display the results.
        For Each workText As String In growingArray
            Console.WriteLine(workText)
        Next workText
        
    End Sub
End Class
1
2
3