B4J Question for each invalid usage?

mc73

Well-Known Member
Licensed User
Longtime User
Hi, running the following I get a casting error (in the tempToggle_selectedChanged sub), while follows a way that does not produce an error. Do I handle it the wrong way in the first sub?
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private aPane As AnchorPane
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
   MainForm.Show
   createExample
End Sub

Sub createExample
   aPane.Initialize ("")
   MainForm.RootPane.AddNode(aPane,0,0,300,140)
   Dim tempToggle As ToggleButton
   Dim tempCombo As ComboBox
   tempToggle.Initialize ("tempToggle")
   tempToggle.Text="toggle"
   aPane.AddNode(tempToggle,0,0,200,60)
   tempCombo.Initialize ("")
   tempCombo.Items.Add("something")
   aPane.AddNode(tempCombo,0,80,200,60)
End Sub

' the following produces a casting error, cause we're trying to set a combo as a toggle
Sub tempToggle_SelectedChange(Selected As Boolean)
   For Each tempT As ToggleButton In aPane
     tempT.Text="ok"
   Next
   
End Sub

' the following works

'Sub tempToggle_SelectedChange(Selected As Boolean)
'   For Each tempT As Node In aPane
'     If tempT Is ToggleButton Then
'     Dim tempT2 As ToggleButton
'     tempT2=tempT
'     tempT2.Text="ok"
'     End If
'   Next
'End Sub
 

LucaMs

Expert
Licensed User
Longtime User
Probably some "node" is not a ToggleButton.
Try to test it with a log (I added one as I would do in B4A, I do not know if it is the same in b4j)

B4X:
'Sub tempToggle_SelectedChange(Selected As Boolean)
For Each tempT As Node In aPane
    If tempT Is ToggleButton Then
       Dim tempT2 As ToggleButton
       tempT2=tempT
       tempT2.Text="ok"
    Else
       log(tempT.GetType)
    End If
  Next
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Hi lucas, your code is already there in mine, commented out :)
My question is whether the collection should be of the same type. If so, I cannot find the "as" purpose..
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Hi lucas, your code is already there in mine, commented out :)
My question is whether the collection should be of the same type. If so, I cannot find the "as" purpose..


I know: i used your code :).

Just a moment, i'm calling my friend, mr Google Translate, and read again your first post :D


Ehm I have read the code! You have also added some ComboBox, so you must use that method.

Unless you fill a List with ToggleButton objects and loop on it ... but it's worth it?
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Ok, 've just checked the syntax over other langs as well. Seems that "as" is there too (though in vb it's optional, and this was the initial reason for posting). In the case of declared objects, to me this would mean that we would iterate through the collection and get just the elements meeting the 'as' criteria. Still surprised a bit, but it's all set now. We can consider the thread answered :)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Luckily you've closed the thread because I did not understand much (because of language, sleep ... or worse? :confused:)

If I remember correctly, in B4A you can declare a variable without type (As), but is "considered" as String. In B4J I don't know.

But even in VB.NET you would get an error (Panel1 contains other types of control):

upload_2014-1-22_2-23-52.png
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My question is whether the collection should be of the same type. If so, I cannot find the "as" purpose..
For Each iterator iterates over all the items in the collection.

Consider this code:
B4X:
Dim list1 As List
... 'we add 10 buttons to the list
For Each b As Button In List1
 'Work with b
Next

If there was no type then we would have need to do something like:
B4X:
For Each o (As Object) In List1
 Dim b As Button = 0
 ...
Next
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I know, Erel. Perhaps I wasn't clear enough. Should be like I wished that java (and other languages as well) had a built in for each, handling the requested elements of a collection meeting certain criteria. For example

B4X:
 for each tempBtn in aCollection where tempBtn is button and tempbtn.tag="something"
...
next

I know it's out of b4j, I was just hoping while dealing with it, that somehow the for each could do this king of things.
 
Upvote 0
Top