Custom types, again

skipper

Member
Licensed User
Hello all,

I'm trying to mimic in B4ppc the VB old style user defined types.

B4X:
type xxx
  aaa as integer
  bbb as string * 12
  ccc as long
  ...
end type

dim pippo as xxx

So, "pippo.aaa" was referring to an integer 2 bytes long, "pippo.bbb" to a string 12 bytes long and so on. Because I need fixed lenght fields to be filled with an ArrayCopy and then converted using the BytesConverter library by Agraham, I was trying to use

B4X:
Dim aaa(4) as Byte
Dim bbb(8) as Byte
Dim ccc(31) as Byte
Dim Type(aaa, bbb, ccc) myVar

in order to get a "myVar.aaa" as an array of Bytes 4 bytes long, "myVar.ccc" as an array of bytes
31 bytes long and so on...

Perhaps I'm wasting my (and your) time. ArrayLen(myVar.aaa) returns 8. What's wrong?

Thanks,
Mimmo
 

agraham

Expert
Licensed User
Longtime User
You have misunderstood what a structure variable is in Basic4ppc. It is an array with predefined constant symbolic accessors. Their names are independent of other variable and array names.
B4X:
Dim aaa(4) as Byte ' array of 4 bytes
Dim bbb(8) as Byte ' array of 8 bytes
Dim ccc(31) as Byte ' array of 31 bytes
Dim Type(aaa, bbb, ccc) myVar ' array of 3 variables aaa=0, bbb= 1 ...
' myVar.aaa is the same as myVar(0)
' myVar.bbb is the same as myVar(1)

ArrayLen(myVar.aaa) returns 8. What's wrong?
I don't know how you get that to run as its errors on my system :confused:
 
Last edited:

skipper

Member
Licensed User
Thank you, Andrew

you are right. I misunderstood the B4PPC online help. Is there a simple way to logically group (one of the advantage of structure) byte variables of different lenght (apart prefixing them with a symbolic string)?

B4X:
Dim Body_aaa(4) as Byte
Dim Body_bbb(8) as Byte
Dim Body_ccc(31) as Byte

Regards
Mimmo
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I would do something like
Code:
Dim Body(4+8+31) As Byte
Int32_aaa = 0
Dbl_bbb = 4
Str31_cccc = 12

....

aaa = Conv.Int32FromBytes(Body, Int32_aaa)
bbb = Conv.DoubleFromBytes(Body, Dbl_bbb)
ccc = Conv.String0FromBytes(Body, Str12_cccc)
Interesting suggestion. I assume that 'conv' is a ByteConverter object.
 
Top