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
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?
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.