我正在尝试创建一个字符串数组,但无法让它正确运行。这就是我所拥有的。
Public Class Form1
Dim wordArray() As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'increase the size of string array by one, by setting the new upperBound at the current Length
'use Preserve so that string currently in the array are not overwritten with Nothing
ReDim Preserve wordArray(wordArray.Length)
'use an TextBox to get the name of the new string from the user
'assign this name (which is a String) to the last element of the string array
wordArray(wordArray.GetUpperBound(0)) = TextBox2.Text
End Sub
End Class
任何帮助将不胜感激,谢谢。
答案 0 :(得分:4)
List(Of String)
怎么样?此处不需要ReDim
。
Private wordList As New List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
wordList.Add(textbox2.Text)
End Sub
答案 1 :(得分:1)
对于固定大小的数组:
Dim strCDRack(0 to 2) As String
strCDRack(0) = "Deftones"
strCDRack(1) = "Tool"
strCDRack(2) = "Disturbed"
对于动态数组:
Dim strCDRack() As String
ReDim strCDRack(0 to 2) As String
strCDRack(0) = "Deftones"
strCDRack(1) = "Tool"
strCDRack(2) = "Disturbed"
用于扩展动态数组:
Dim strCDRack() As String
ReDim strCDRack(0 to 2) As String
strCDRack(0) = "Deftones"
strCDRack(1) = "Tool"
strCDRack(2) = "Disturbed"
ReDim Preserve strCDRack(0 to 3) As String
strCDRack(3) = "Charlotte Church"
有关VB数组的详细信息,请查看此link ..
答案 2 :(得分:0)
使用清单!
Dim StringArray As New List(Of [String])()
在点击处理程序中:
StringArray.Add(TextBox1.Text)
答案 3 :(得分:0)
你应该像Collections
一样使用List(Of String)
,它随着元素数量的增长而增长。无需自己维持规模。当你ReDim
尝试增加数组大小时,问题可能在你的最后。
ReDim Preserve wordArray(wordArray.Length + 1)