usage of custom types

anonim

New Member
I have the following
B4X:
' Used as a way of transfering data from one sub to the other.
   Dim Type(ID, StackNo, OperatorID, CraneID, JobID, JobStartTime, JobEndTime) CraneJob
Sub UploadOneJobData(job As CraneJob)
...
Compiler complains about parameter 'job' of the procedure UploadOneJobData. How should I specify the type of variable in this case?
 

derez

Expert
Licensed User
Longtime User
The structure is global, like all arrayes, and must be declared there. You can re-declare them in a sub but only to change the dimension. So you can use the data without need to define the sub with parameters.

Of course, since it is global, changes inside the sub will have a global effect.

Also - you are not allowed to use a parameter with the same name of a global variable, like in this case.
 
Last edited:
This is the same issue I am facing. I have made a post in the wishs forum. We need to be able to pass and return structures.
 

anonim

New Member
OK, I understand now that it is not possible to pass a structure (in C terms)/ record (in Pascal terms). I don't know basic very much...
 

mjcoon

Well-Known Member
Licensed User
That seems rather limiting. :sign0161:

So is it possible to assign the whole of a structure with one statement so that a global structure can act as a Sub parameter (but unfortunately not placed there in the syntax) and thus the Sub can be called to act upon multiple structures?

(Of course if each element of the structure has to be assigned separately it would be more laborious than using multiple parameters!)

Mike.
 

agraham

Expert
Licensed User
Longtime User
So is it possible to assign the whole of a structure with one statement
Yes. Arrays can accept assignments to array references returned from libraries as well as internal arrays.

B4X:
Sub Globals
   Dim Type(a, b, c)a(10)
   Dim b(0,0)
   ' dimensions of b() don't actually matter in the IDE
   ' but the optimising compiler can get confused if you change
   ' the dimensionality of an array
End Sub

Sub App_Start
   a(0).a = 23
   b() = a() ' assign a reference to a() to b()
   Msgbox(b(0,0) & " is also " & a(0).a)
End Sub
 

mjcoon

Well-Known Member
Licensed User
b() = a() ' assign a reference to a() to b()

"Reference" - are the two names now synonymous? Referring to a single area of data storage?

That would be desirable to allow a global structure to be used as if it were a Sub parameter that could be called by reference. It could be used to output data from the Sub too, as one might do in other languages. (Being able to make this intention explicit by having a better syntax and corresponding scoping rules would be much preferable!)

I'm familiar with the use of arrays in Basic4ppc to get output from the serial port, for instance. But it seems surprising to find that the same thing can be done with what were declared as distinct arrays or structures.

I think I'll make cautious use of this feature!
 

agraham

Expert
Licensed User
Longtime User
"Reference" - are the two names now synonymous? Referring to a single area of data storage?
Yes.
But it seems surprising to find that the same thing can be done with what were declared as distinct arrays or structures.
Why surprising? Yes they were originally two distinct areas of memory with two separate pointer variable values. But assigning one pointer variable to another means that they both now reference the same memory area so dereferencing either allows access to the same values. In .NET the now "orphan" original area which is no longer referenced is automatically garbage collected for re-use.
 

mjcoon

Well-Known Member
Licensed User
Yes.
Why surprising? Yes they were originally two distinct areas of memory with two separate pointer variable values. But assigning one pointer variable to another means that they both now reference the same memory area so dereferencing either allows access to the same values. In .NET the now "orphan" original area which is no longer referenced is automatically garbage collected for re-use.

Surprising only in the sense that the declarations don't look very distinct from those of ordinary variables that do not behave as pointers. Surprise arises from the expectations of the observer, of course, and mine arises because Basic4PPC doesn't say much about references to objects (except that Sub parameters cannot be one!). I know that strings are dynamic, and must be iplemented as pointers. But I'm confident that when one string is assigned to another it is the content that is copied. I suppose the rule is: beware of empty parentheses/braces!

I must raise (elsewhere) another surprise that the "To" expression of a For statement seems to be evaluated at the start of the 1st iteration and not again. If true, as my failed usage suggests, this would be worth a mention in the semantic description. And I expect that the same would apply to the Step expression.
 

agraham

Expert
Licensed User
Longtime User
But I'm confident that when one string is assigned to another it is the content that is copied.
Not true I'm afraid. Strings are reference types in .NET. However they don't look like it because in .NET strings are immutable so if you alter a string you actually get a reference to another string in a different place in memory.
B4X:
Dim aa(10) ' the array variable aa holds a reference as its value
Dim ab(0)  ' so does ab
...
a = "a string" ' the variable a holds a reference to a string as its value
b = a ' actually copies the reference so both a & b point to same string
b = StrReplace(b, "a", "A") ' makes a new string. a and b are now different references.
' a points to "a string" and b to "A string"

ab() = aa() ' copies an array  reference i.e. the value in aa is assigned to ab
a = ab(0) ' dereferences the array reference held in ab to get a value
 

mjcoon

Well-Known Member
Licensed User
Thanks very much for that clarification, Andrew. That "immutable" makes all the difference!

Entirely OT: you mention elsewhere IIRC that the 1st computer you got to handle used germanium transistors. I had forgotten, but have just looked up, that my first (ICT 1301) did too! But I was only playing with them, while visiting ICT's final test bay as an apprentice...
 

mjcoon

Well-Known Member
Licensed User
Ours were deadly serious. Ferranti Argus 350s running anti-submarine helicopter, frigate and SSN/SSBN nuke trainers for the Royal Navy in the late 60s/early 70s.

I hope you don't have to kill me now you've told me that! By the time I got to Ferranti Argus it would have been the mid 1970s and the Argus 700, in silicon!
 
Top