Android Question how to create dynamic variables

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Hello friends

I need to send a list of products in Bytes to a thermal printer connected by bluetooth.

To make this possible, I send the data by line of bytes to the printer. For this I create a variable type Byte using this code:

B4X:
Dim Line1Byte () as Byte

The problem is that I do not know how many Byte variables I should create since the list of products can have from 1 product to N products.

I wrote this code but it gives me error:

B4X:
For m = 0 To ListProd.Size-1
Dim Line&m&Byte () As Byte
Next

In the end, I need to create the same number of Byte variables that are in the list of products

I need to know if in B4A it is possible to create these variables in a dynamic way.

I am very grateful with the solution that you can give me.
 

KZero

Active Member
Licensed User
Longtime User
B4X:
'to store bytes line
    For m = 0 To ListProd.Size-1
        Dim LineByte() As Byte '= your bytes
        List1.Add(LineByte)
    Next
    
    'to get bytes line
    Dim LineByte() As Byte
    LineByte = List1.Get(1) ' 1 is your line number
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Thank @KZero

What I need, if I have 10 products in the list, for example, I have to create 10 Byte-type variables.

The problem is that the list of products can have from 1 product to N products

Thank you
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
its more a class object, you have a product class and a function/sub that make this bytes for the printer or u store the bytes direct there.
this product object variable you can hold in a list and the source from this maybe comes from a sqlite database or server side database.

or store a product in a type / struct
B4X:
Type Product(Name As String,LineBytes() As Byte)

B4X:
    Dim Products As List
    Products.Initialize
    Dim ProductX As Product
    ProductX.Name = "ABC"
    Dim Bytes(100) As Byte
    ProductX.LineBytes = Bytes
    Products.Add(ProductX)
 
Last edited:
Upvote 0
Top