B4R Question Can I use Structure in B4R?

Alpandino

Member
Licensed User
Hi,
I know I can create new Type, but now I need also to create method/function for my type. This could simplify my job.
I'd like to avoid writing a new lib for this, so I'd like to know if I can use the Structure syntax, more or less in this way:

B4X:
Public Structure Box
       Dim Length As Double
       Dim Width As Double
       Dim height As Double

       Function Volume() As Double
           Return Length * Width * height
       End Function
End Structure

Is it possible to do something similar in B4R?

Thank you
 

Alpandino

Member
Licensed User
My case is a more complicated.
In a function of my custom type I need to modify some parameters of the custom type itself.
Here an example (the actual code is more complicated, but the idea is the same):
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Type Box (x As Int, y As Int, z As Int)
    Dim x, y, z As Box
  
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
End Sub

Sub BoxVolume(bx As Box) As Double
    Return bx.x * bx.y * bx.z
End Sub

Sub BoxEnlarge(bx As Box)
    bx.x = bx.x*2
    bx.y = bx.y*2
    bx.z = bx.z*2
End Sub

The "BoxEnlarge" Sub isn't really able to change x, y, z value, because I can't pass bx for reference.
Is it possible to achieve this without a library?

Thanks
 
Upvote 0

Alpandino

Member
Licensed User
As I previously wrote, a library is not relevant here.

I don't understand the problem. Why can't you call:
B4X:
BoxEnlarge(x)
?

Objects are passed by reference.
Really? Object are passed by reference? I read in the beginners guide that in B4R I can't pass any for reference....
 
Upvote 0

JanG

Member
Licensed User
Longtime User
Yes, it works. Declare a Type with one element (your var-type). And declare a variable of this new type. Then use this "type-variable" as parameter. It is now by reference.
 
Upvote 0
Top