B4R Question Can I use Structure in B4R?

Discussion in 'B4R Questions' started by Alpandino, Sep 10, 2018.

Tags:
  1. Alpandino

    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:

    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    1. Creating a new library for this is a mistake.
    2. Custom types cannot have methods.

    3. You can do something like:

    Code:
    Sub BoxVolume(bx As Box) As Double
     
    Return bx.Length * bx.Width * bx.Height
    End Sub
     
  3. Alpandino

    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):
    Code:
    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
     
  4. tigrot

    tigrot Well-Known Member Licensed User

    Use global variables as results.
    Instead of assign result to say: bx.x=bx.x*2
    Write x.x=bx.x*2 and the result is available in x.x
     
    Last edited: Sep 11, 2018
  5. Erel

    Erel Administrator Staff Member Licensed User

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

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

    Objects are passed by reference.
     
  6. Alpandino

    Alpandino Member Licensed User

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

    JanG Member Licensed 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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Really.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice