New feature - inline casting

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code: Copy
Dim Buttons As List = Array(Button1, Button2, Button3, Button4, Button5)
Dim btn As B4XView = Buttons.Get(1)
btn.Text = "boring" 

Buttons.Get As B4XView(2).Text = "less boring"
Button5.Text = Buttons.Get As B4XView(2).Text
 Dim m As Map = CreateMap("ttt": " 123 ;lkasd  ")
Button4.Text = m.Get As String("ttt").Trim
Root.GetView As WebView(8).LoadURL("https://www.b4x.com") '8 is the view's index
 
'8 is the view's index
A numbering in the designer would be very useful to find out faster the index of a view, with 20 views this is often exhausting.
0
1
2
___0
___1
___2
3
4
 
Hi @Erel is very interesting.
On which version will it be available ( B4A/B4i/B4J?) ?
Will there be any examples ?
Thank you
 
Great!! Does it also affect non-indexed variables?

Example
B4X: Copy
Sub TicToc(Ob as object)
     Ob as B4xView.Text = "try"
End sub

B4X: Copy
if Sub TicTac(Ob as object)
     if Ob as B4xView.Text = "try" then log("ok")
End sub

if Sub TicTec(Ob as String)
     if Ob as int = 12 then log("12")
End sub
 
Last edited:
I know you will have made up your mind and this might not fit your parser easily but I would find it more intuitive as
B4X: Copy
Dim Buttons As List = Array(Button1, Button2, Button3, Button4, Button5)
Dim btn As B4XView = Buttons.Get(1)
btn.Text = "boring" 

(Buttons.Get(2) As B4XView).Text = "less boring"
Button5.Text =(Buttons.Get(2) As B4XView).Text
 Dim m As Map = CreateMap("ttt": " 123 ;lkasd  ")
Button4.Text = m.Get As String("ttt").Trim
(Root.GetView(8) As WebView).LoadURL("https://www.b4x.com") '8 is the view's index
 
A numbering in the designer would be very useful to find out faster the index of a view, with 20 views this is often exhausting.
0
1
2
___0
___1
___2
3
4

@Erel I would request you to consider @Alexander Stolte request too, if possible. As I too have to count them one by one.
 
Great!! Does it also affect non-indexed variables?
Currently it can only be added after methods.
A numbering in the designer would be very useful to find out faster the index of a view, with 20 views this is often exhausting.
It isn't really related to this thread but it will be solved in one of the next versions.

On which version will it be available ( B4A/B4i/B4J?) ?
The next version of each IDE.
 
Code: Copy
Dim Buttons As List = Array(Button1, Button2, Button3, Button4, Button5)
Dim btn As B4XView = Buttons.Get(1)
btn.Text = "boring"

Buttons.Get As B4XView(2).Text = "less boring"
Button5.Text = Buttons.Get As B4XView(2).Text
Dim m As Map = CreateMap("ttt": " 123 ;lkasd  ")
Button4.Text = m.Get As String("ttt").Trim
Root.GetView As WebView(8).LoadURL("https://www.b4x.com") '8 is the view's index
Wait, is this B4X version 2.0? ?

What I know is we use “As” when declaring a variable with Public/Private/Dim or when defining the return type of a function.
1. I never know we can declare something As array(x) and directly to it’s property.
2. I think I need a new introduction to .Get and .Get(x) methods.
3. SomeMap.Get As String("xxx").Trim is crazy. What is this type String(“Some Text”)?
4. Root.GetView As WebView(8).LoadURL("http...”)?
Oh no... my brain can’t process anymore!!!
Object.Method As View(i).Method(“xxx”) ?

Now, I am new to B4X. ?
 
Yeah, I also feel very confused by this new feature.
I think the feature will make that a little easier and more readable:
B4X: Copy
xpnl_item_3.GetView(0).Color = BackgroundColor
xpnl_item_3.GetView(1).GetView(0).Text = Text
xpnl_item_3.GetView(1).GetView(1).GetView(0).Text = Amount & "€"
xpnl_item_3.GetView(1).GetView(1).GetView(0).Text = xpnl_item_3.GetView(1).GetView(1).GetView(0).Text.Replace(".",",")
xpnl_item_3.GetView(4).GetView(0).Text = GetItem3Count(CardType) +1
And if I have a customview, I have to declare a variable because otherwise I can't access the properties. And through the feature this is simplified, I think.
 
Personally, I find that the syntax suggested by @agraham is more readable
Code: Copy
(Buttons.Get(2) As B4XView).Text = "less boring"

But, then, shouldn't it be
Code: Copy
Dim m As Map = CreateMap("ttt": " 123 ;lkasd  ")
Button4.Text = (m.Get("ttt")As String).Trim

Anyway, I'm sure that once we get used to this new feature we will love it (as it always happened in the past..)
Go B4X, go!
 
Personally, I find that the syntax suggested by @agraham is more readable
Yes, I find it to be a big improvement.

But, then, shouldn't it be
Yep, it should. And that's also a big improvement in my opinion.

Anyway, I'm sure that once we get used to this new feature we will love it
I'm not so sure, I'm really trying to like the syntax that Erel showed, but I'm not feeling it yet.

Go B4X, go!
Agreed! :)
 
is this limited to Views or will this be possible too?

MyClass:
Code: Copy
Sub Class_Globals
    Public Name As String
End Sub

Public Sub Initialize(mName as String)
    Name = mName
End Sub

Code: Copy
Dim lst as List
lst.Initialize

for i = 0 to 5
    Dim cls as MyClass
    cls.Initialize("Name" & i)
    lst.Add(cls)
next

for i = 0 to lst.Size - 1
    Log(lst.Get as MyClass(i).Name)
next

My preference would've been:
Code: Copy
for i = 0 to lst.Size - 1
    Log((MyClass)lst.Get(i).Name)
next

But I also do like @agraham's suggestion a lot!
Code: Copy
for i = 0 to lst.Size - 1
    Log((lst.Get(i) As MyClass).Name)
next

EDIT: The more I look into Erels implementation, the more I start to like it! It will probably even be the easiest one to parse for e.g. BANano ?, as for JavaScript the Type is irrelevant and can just be ignored anyway:

Code: Copy
for (var i = 0; i < lst.length; i++)  {
    console.log(lst[i]["Name"]);
}

Alwaysbusy
 
Last edited:
I know you will have made up your mind and this might not fit your parser easily but I would find it more intuitive as
B4X: Copy
Dim Buttons As List = Array(Button1, Button2, Button3, Button4, Button5)
Dim btn As B4XView = Buttons.Get(1)
btn.Text = "boring"

(Buttons.Get(2) As B4XView).Text = "less boring"
Button5.Text =(Buttons.Get(2) As B4XView).Text
Dim m As Map = CreateMap("ttt": " 123 ;lkasd  ")
Button4.Text = m.Get As String("ttt").Trim
(Root.GetView(8) As WebView).LoadURL("https://www.b4x.com") '8 is the view's index
It is such a special thing that it would be even better to use square brackets (although typing these is more cumbersome).
B4X: Copy
[Buttons.Get(2) As B4XView].Text = "less boring"
 
Back
Top