Dynamic arrays
简述
Dim myDynamicArray() As String
ReDim [ Preserve ] arrayName ( bounds ) [ As dataType ]
Option Base 1
' Declare a dynamic String array. Later, this is
' defined as a one-dimensional array whose elements
' are assigned values that the user enters.
Dim myNames() As String
Dim ans1 As Integer
Dim ans2 As Integer
Dim counter As Integer
Dim userInput As String
' Ask the user to enter a number and assign it to ans1%.
ans1% = CInt(InputBox$ _
("How many names would you like to enter?"))
' Use ans1% as the upper bound of the array's only dimension.
ReDim myNames(ans1%)
' Elicit ans1% strings from the user, and assign them
' to successive elements in the array.
For counter% = 1 to ans1%
myNames(counter%) = InputBox$("Enter a name: ")
Next
' Print the contents of the array on a single line
' with a space between the value of each element.
For counter% = 1 to ans1%
Print myNames(counter%) " " ;
Next
' Output: a newline
Print ""
' Ask the user for another number and assign it to ans2%.
ans2% = CInt(InputBox$("How many more names?"))
' If the number is greater than 0, resize the
' array, preserving its original values, so that the
' user can enter additional values.
If ans2% > 0 Then
ReDim Preserve myNames(ans1% + ans2%)
' Elicit the new values and assign them to the
' elements that have been allocated after the old ones.
For counter% = 1 to ans2%
myNames(counter% + ans1%) = InputBox$("Enter a name: ")
Next
' Print the contents of the array on a single line
' with a space between the value of each element.
For counter% = 1 to ans1% + ans2%
Print myNames(counter%) " " ;
Next
Print ""
End If
domino创建动态数组
最新推荐文章于 2022-05-09 16:18:53 发布