Android Question Initialize data in multidimensional array, list, map at declaration

RJB

Active Member
Licensed User
Longtime User
I'm sure the answer is simple (but I've tried all combinations I can think of) and well documented here somewhere (but I haven't been able to find it).

With a simple array you use dim x() as int = (1,2,3) at declaration, but what if you want to initialise the data in a multi dimensional array, list or map and also if you want to initialise with a Type?
B4X:
 Type mytype(a As Int, b As String)
 Dim MyytpeToo() As mytype = {(3, "2"), (4, "5")}
(I know you can interate to set the data so that's not what I'm looking for, just a simple way to set the data at declaration)

Thanks
 

LucaMs

Expert
Licensed User
Longtime User
dim x() as int = (1,2,3)
This must be:
B4X:
dim x() as int = Array As Int (1,2,3)


For that type you could use:
B4X:
    Type mytype(a As Int, b As String)

    Dim My1 As mytype
    My1.Initialize
 
    Dim My2 As mytype
    My2.Initialize
    My2.a = 20
    My2.b = "200"
 
    Dim MyytpeToo As Map
    MyytpeToo = CreateMap("First":My1, "Second":My2)

    Dim obj As mytype = MyytpeToo.Get("Second")
    Log(obj.a)
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
thanks LucaMS
the int initialization example was a typo! That is well documented and works for all single dimensional arrays.

I was looking for something like that for multi-dimensional arrays, e.g. dim x(,) = array as int{(1,2), (3,4)} etc. but I guess that doesn't exist?
or the same for maps/ lists.
I guess I'm stuck with using a single dimensional array and indexing into it.
e.g.
B4X:
Sub KeyboardButtons_Click
Dim B As Button = Sender
If KeyInfoStrings((B.Tag * 10) + KeyEvent) <> "" Then
CallSub(Me, KeyInfoStrings((B.Tag * 10) + KeyEvent) & "_Click")
Return
End If

etc.

thanks again.

the "hover over 'mytype'" doesn't seem to work - is that in a version of b4a later than V9.0? I haven't upgraded yet.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
is that in a version of b4a later than V9.0?
yes, added in 9.8

 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
"upgrade, it's free now" - story of my life. Product made free when I've already paid for it! :) Just haven't managed to find time to upgrade yet!
"I don't like so much your code; you should call "normal" routines, not GUI event routines." It's not a GUI routine. It's my own _Click handler for a custom keyboard. Individual keys/ shift/ control/ etc. and the event name are held in the array, which is why an easy way to populate a multi-D array, list or map of types would have been useful. The array defines the keys and the layout is inferred from the number of keys/ rows/ columns. I'll probably put it in Code Snippets when finished. Might save others a lot of work.
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Yes, but in this case KeyEvent is an int and ((B.Tag * 10) + KeyEvent) is an index into the single dimensional array KeyInfoStrings.

B4X:
 Dim const KeySymbol As Int = 0
 Dim const NormalCode As Int = 1
 Dim const ShiftCode As Int = 2
 Dim const CapsCode As Int = 3
 Dim const ShiftCapsCode As Int = 4
 Dim const CtrlCode As Int = 5
 Dim const FnCode As Int = 6
 Dim const AltCode As Int = 7
 Dim const AltGrCode As Int = 8
 Dim const KeyEvent As Int = 9

Dim KeyInfoStrings() As String = Array As String ("Esc", "Esc", "Esc", "Esc", "", "", "", "", "" , "Esc", _ 'Row 1
              "F1", "F1", "F1", "F1", "", "", "", "", "" , "F1", _
              "F2", "F2", "F2", "F2", "", "", "", "", "" , "F2", _
………….
              "¬" & CRLF & "`  ¦", "`", "¬", "`", "¬", "", "", "", "" , "", _ 'Row 2
              "!" & CRLF & "1", "1", "!", "1", "", "", "", "", "" , "", _
              Chr(34) & CRLF & "2", 2, Chr(34), "2", Chr(34), "", "", "", "" , "", _
…………..
              "A", "a", "A",         etc.



Sub KeyboardButtons_Click     ' Built in _Click event
  Dim B As Button = Sender 
  If KeyInfoStrings((B.Tag * 10) + KeyEvent) <> "" Then 
         CallSub(Me, KeyInfoStrings((B.Tag * 10) + KeyEvent) & "_Click")     ' my "normal routine"
         Return 
  End If 

etc.


End Sub

KeyInfoStrings define the layout and characters for the keys. QWERTY, AZERTY, Numeric or whatever.
Hence it would have been nice to have been able to have used an array/ map/ list of type, or at least a multi-dimensional array, but it's not worth loading the data into one when the KeyInfoStrings can be used directly.
 
Upvote 0
Top