标签:vb 数组 初始化 vba visual basic
vb中数组的初始化
问题:
计划使用数组存储以下值:
"零分,班序,年名,序,组名,级名,总分3,总分5,总分9,总序,语序,数序,英序,物序,化序,政序,历序,地序,生序"
方法:
在vb中数组初始化比较麻烦
1.标准做法:
dim field(19) as string ‘需要手工计算下标上限 fiels(0)="零分" ‘逐行赋值 field(1)="班序" ... field(18)="生序"
很啰嗦,一句一行赋值语句.
2.改进一下:
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.pring sField(i) next i
上例将一个由","分隔的字符串构造出数组.
3.再改进一下:
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.pring sField(i) next i
上例使用一个split函数构造出数组.
结论:
使用split函数构造数组的值,快速,简洁.
本文出自 “清风乱翻书” 博客,请务必保留此出处http://lioncn.blog.51cto.com/1557898/1567434
标签:vb 数组 初始化 vba visual basic
原文地址:http://lioncn.blog.51cto.com/1557898/1567434