其实我的程序是这样的:某通信协议的数据包是一个定长的33字节数据块,分为几个区域,如开始字符,读写指示标识,目的地址,数据区域,校验区域,结束字符等等。基本上全是以ASCII来表示的。其中最长的区域就是数据区域了,共十六个字节。于是我自己定义了一个结构: type commdn fldstart as byte fldmod as byte fldaddrofstation(2) as byte fldaddrofreg(8) as byte fldcount(2) as byte flddata(16) as byte fldbcc(2) as byte fldend as byte end type 接下来就是往这个结构里填充数据了,可是我直接这样写: dim writedata as commdn ... writedata.flddata="0000000100000000" ... 程序就报错“不能给数组赋值”,可是我一个个赋值我嫌太麻烦,所以请教有没有什么简单的办法可以向这个结构里填充数据?
dim a(100) as Byte dim i for i = 0 to 100 a(i) = ????? next i
大概没有什么更好的办法。 写成函数,只是稍微减少一些代码量,并没有改变一个一个赋值的规律:public function Array(theArray() as byte, theValue as byte) as byte 'theArray是要赋值的数组,theValue是要赋予的值。 dim i as long for i = lbound(theArray) to ubound(theArray) theArray(i)= theValue next i Array = theArray end function
以下就是我写的函数: Sub Str2ByteArray(bArray() As Byte, Length As Byte, Str As String) Dim i As Byte For i = 0 To Length - 1 bArray(i) = Asc(Mid(Str, i + 1, 1)) Next End Sub
a=“0000000100000000”
同意 zwbhyy(博)和icansaymyabc(学习与进步),你可以有循环和其它技巧
type commdn
fldstart as byte
fldmod as byte
fldaddrofstation(2) as byte
fldaddrofreg(8) as byte
fldcount(2) as byte
flddata(16) as byte
fldbcc(2) as byte
fldend as byte
end type
接下来就是往这个结构里填充数据了,可是我直接这样写:
dim writedata as commdn
...
writedata.flddata="0000000100000000"
...
程序就报错“不能给数组赋值”,可是我一个个赋值我嫌太麻烦,所以请教有没有什么简单的办法可以向这个结构里填充数据?
dim i
for i = 0 to 100
a(i) = ?????
next i
写成函数,只是稍微减少一些代码量,并没有改变一个一个赋值的规律:public function Array(theArray() as byte, theValue as byte) as byte
'theArray是要赋值的数组,theValue是要赋予的值。
dim i as long
for i = lbound(theArray) to ubound(theArray)
theArray(i)= theValue
next i
Array = theArray
end function
Sub Str2ByteArray(bArray() As Byte, Length As Byte, Str As String)
Dim i As Byte
For i = 0 To Length - 1
bArray(i) = Asc(Mid(Str, i + 1, 1))
Next
End Sub