This does not work:
dim bytes() as byte = Array( &h00, &h01, &h02, &h03, &h04 )
The compiler gives:
Type Mismatch error. Expected byte() but got UInt32().
I know there are other ways to create the array, but it bugs me that this way doesn’t work, because it really seems like it should.
From the docs:
[quote]Assign a number using a hex literal:
Dim hex As Integer
hex = &hff ’ hex = 15 as a decimal integer
[/quote]
Integers are 32 or 64 bytes, are they not?
having said that, (1,2,3) generates the same message, and so does (byte(1),byte(2),byte(3))
so thats a red herring.
Yep, I forgot to mention that I tried casting within the array, and it still didn’t work.
Arrays don’t auto-convert between types. Since &h00 is a UInt32 it doesn’t match the type of Byte.
You can override that using CType, but that is a lot of typing:
Dim bytes() As Byte = Array( CType(&h00, Byte), CType(&h01, Byte), CType(&h02, Byte), CType(&h03, Byte), CType(&h04, Byte) )
I see. Thank you for the answer, Paul.
I suppose making my own ByteArray class would make sense at this point. I’ll do that.
It may be enough to only CType first value.
how about:
Function ArrayByte(ParamArray nums As UInt32) As Byte()
Dim ret() As Byte
For Each n As UInt32 In nums
ret.Append n
Next
Return ret
End Function
'...
dim bytes() as byte = ArrayByte( &h00, &h01, &h02, &h03, &h04 )