vb中数组的初始化
问题:
计划使用数组存储以下值:
"零分,班序,年名,序,组名,级名,总分3,总分5,总分9,总序,语序,数序,英序,物序,化序,政序,历序,地序,生序"
方法:
在vb中数组初始化比较麻烦
1.标准做法:
dim field(19) as string '需要手工计算下标上限
field(0)="零分" '逐行赋值
field(1)="班序"
...
field(18)="生序"
for i=0 to 19
debug.print field(i)
next i
很啰嗦,一句一行赋值语句.
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.print 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.print sField(i)
next i
上例使用一个split函数构造出数组.
结论:
使用split函数构造数组的值,快速,简洁.
转载于:https://blog.51cto.com/lioncn/1567434
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/34322.html