TomA Active Member Licensed User Longtime User Nov 16, 2013 #1 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?
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?
Erel B4X founder Staff member Licensed User Longtime User Nov 17, 2013 #2 Code: Copy Sub CreateMyType (a As Int, b As Int, c As String) As MyType Dim m As MyType m.Initialize m.a = a ... Return m End Sub Dim MyTypeArray() As MyType = Array As MyType(CreateMYType(...), CreateMyType(...), ...) Upvote 0
Code: Copy Sub CreateMyType (a As Int, b As Int, c As String) As MyType Dim m As MyType m.Initialize m.a = a ... Return m End Sub Dim MyTypeArray() As MyType = Array As MyType(CreateMYType(...), CreateMyType(...), ...)
TomA Active Member Licensed User Longtime User Nov 17, 2013 #3 Thank-you Erel - that will do it. So simple when you know how!! Upvote 0
SteveTerrell Active Member Licensed User Longtime User Feb 16, 2016 #4 TomA said: Thank-you Erel - that will do it. So simple when you know how!! Click to expand... This does not seem to work in a Code Module Code: Copy '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 Code: Copy 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
TomA said: Thank-you Erel - that will do it. So simple when you know how!! Click to expand... This does not seem to work in a Code Module Code: Copy '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 Code: Copy 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?
Erel B4X founder Staff member Licensed User Longtime User Feb 16, 2016 #5 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. Upvote 0
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.
SteveTerrell Active Member Licensed User Longtime User Feb 16, 2016 #6 Erel said: 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. Click to expand... 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
Erel said: 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. Click to expand... 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).