vb中數組的初始化數組
問題:
ide
計劃使用數組存儲如下值:函數
"零分,班序,年名,序,組名,級名,總分3,總分5,總分9,總序,語序,數序,英序,物序,化序,政序,歷序,地序,生序"
方法:
spa
在vb中數組初始化比較麻煩debug
1.標準作法:字符串
dim field(19) as string '須要手工計算下標上限 field(0)="零分" '逐行賦值 field(1)="班序" ... field(18)="生序" for i=0 to 19 debug.print field(i) next i
很囉嗦,一句一行賦值語句.
string
2.改進一下:it
Dim sFieldList As String Dim sField(1 to 19) As String '須要手工計算下標上限 Dim i As Integer Dim nBgn As Integer, nLen As Integer '注意尾部有',不可省略 sFieldList = "零分,班序,年名,序,組名,級名,總分3,總分5,總分9,總序,語序,數序,英序,物序,化序,政序,歷序,地序,生序," nBgn = 1 For i = 1 To 19 nLen = InStr(nBgn, sFieldList, ",") sField(i) = Mid(sFieldList, nBgn, nLen - nBgn) nBgn = nLen + 1 debug.print sField(i) next i
上例將一個由","分隔的字符串構造出數組.class
3.再改進一下:List
Dim sFieldList As String Dim sField() As String '動態數組 Dim i As Integer '注意尾部',無關緊要 sFieldList = "零分,班序,年名,序,組名,級名,總分3,總分5,總分9,總序,語序,數序,英序,物序,化序,政序,歷序,地序,生序," sField = Split(sFieldList, ",") For i = 0 To UBound(sField) - 1 debug.print sField(i) next i
上例使用一個split函數構造出數組.
結論:
使用split函數構造數組的值,快速,簡潔.