Wish Provide Non primitive members of custom types - Like C/C++ Structures.

rgarnett1955

Active Member
Licensed User
Longtime User
Hi

I have a work around. It seems like you can pass the results of non-primitive function arg's via the arg's temselves as they appear to be arg's referenced not by value.

Is this correct?


I often wish to create a list or array with meta data fields using a function:

Non primitive members of custom types:
Sub Class_Globals  
    Type win_t(numNaN As Int, numHighExceptions As Int, numLowExceptions As Int, avg As Double, w As List)
   
    Private w As win_t   ' window Buffer
End Sub  

    ....
    w = fillWindowDoAverage(dataBuffer, 0, nW)
    ....
   
   
'==========================================================================================
Private Sub fillWindowDoAverage(data() As Double, startIndex As Int, nwin As Int) As win_t
    Dim listOut As win_t
    Dim nNan As Int = 0
    Dim val As Double
    Dim avgAcc As Double = 0
   
    listOut.Initialize
    listOut.w.Initialize
   
    If startIndex + nwin > data.Length - 1 Then
        Return listOut
    End If
   
    For i = 0 To nwin - 1
        val = data(i)
        If IsNaN(val) = True Then
            nNan = nNan + 1
        Else
            avgAcc = avgAcc + val
        End If
        listOut.w.Add(val)
    Next
   
    listOut.numNaN = nNan
    listOut.avg = avgAcc / nNan
    Return listOut
End Sub

I may be barking up the wrong tree, there might be a way of achieving this that I haven't thought of

Best regards Rob
 
Last edited:

agraham

Expert
Licensed User
Longtime User
I don't understand your wish as custom types can already include non-primitive items. Indeed you do in your example code.

Also Java, and hence B4X can only pass parameters by value.

In the case of a non-primitive type the value of a variable is a reference to the object instance. The point of pass by value is that a called function cannot change the value of the original variable. However if the value is a reference it can modify the object whose reference it is.

The fact that an instance reference is passed is still a pass by value. Passing by value is defined as passing the contents of a variable whereas pass by reference passes a reference to the variable. The fact that a variable of a type of some object holds a reference means that passing by value passes that reference.

Discussion of it in this thread.
 
Top