Android Question Assigning value to array of user defined type

TomA

Active Member
Licensed User
Longtime User
It may not be possible to do what I would like to do, but I thought I would ask anyway.
I know that I can assign values to a string, int, etc. array with code something like:

Dim MyArray() as String
MyArray = Array as String("String1", "String2", "String3")

but is there any way to assign values to an array of a user defined type:

Type MyType(a as Int, b as Int, c as String)
Dim MyTypeArray() as MyType
MyTypeArray = Array as MyType( ???? ...

The actual type I am defining contains 7 ints and 2 strings and it would be nice (take less memory and be faster) if the values could be assigned this way instead of having to to it in code for each item in the array.

Is it possible and, if so, how?
 

TomA

Active Member
Licensed User
Longtime User
Thank-you Erel - that will do it. So simple when you know how!!:)
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Thank-you Erel - that will do it. So simple when you know how!!:)

This does not seem to work in a Code Module

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

   
Type testType ( s As String)
Public const ttArray() As testType = Array As testType( iTestType("S"))

End Sub

Sub iTestType(s As String) As testType
Dim temp As testType
temp.Initialize
temp.s = s
Return temp
End Sub

Compile and Rapid Debug reports

B4X:
B4A version: 5.50
Parsing code.    (0.00s)
Compiling code.    (0.06s)
Compiling layouts code.    (0.00s)
Generating R file.    (0.14s)
Compiling debugger engine code.    Error
B4A line: 9
Public const ttArray() As testType = Array As test
javac 1.7.0_45
shell\src\b4a\example\testcode_subs_0.java:46: error: cannot find symbol
testcode._ttarray = RemoteObject.createNewArray("b4a.example.testcode._testtype",new int[] {1},new Object[] {_itesttype(_ba,RemoteObject.createImmutable("S"))});
                                                                                                                        ^
  symbol:   variable _ba
  location: class testcode_subs_0
1 error

Is there any way of getting round this?
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
You cannot call a different sub from Process_Globals.

Process_Globals should only be used to declare variables. Assignments of primitives and strings is also allowed.

Thanks,

Just for information..

That error does not seem to be spotted when the array is (incorrectly) initialised in Main Process_Globals. The app just terminates (or fails to run after installing).
 
Upvote 0
Top