Android Question How to create a code like this?

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Is it possible to create a code in B4X that does something like this?

B4X:
Dim sb As StringBuilder
sb.Initialize
   
sb.Append("A").Append("B").Append("C)" ... ... ...
(Infinite times)

What is this called in programming?
 

DonManfred

Expert
Licensed User
Longtime User
What is this called in programming?
Do you mean the builderpattern? If yes. I dont know if it is possible with b4a.
It is possible with java and probably inline java
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
I do not know the name.
This is really cool.

For example.
B4X:
'Pseudo code

Dim AddTool1 As AddTool
AddTool1.initialize

AddTool1.AddThis(10).AddThis(20).addThis(50)

Log(AddTool1.Total)

//80


I tried some ways to create something lwith B4X but it did not work.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Add a class with this code:

B4X:
Sub Class_Globals
    Private mValue As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    mValue = 0
End Sub

Public Sub AddThis(Value As Int) As AddTool
    mValue = mValue + Value
    Return Me
End Sub

Public Sub Total As Int
    Return mValue
End Sub

Edit: It is hard to give an answer faster than Erel. :)
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Add a class with this code:

B4X:
Sub Class_Globals
    Private mValue As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    mValue = 0
End Sub

Public Sub AddThis(Value As Int) As AddTool
    mValue = mValue + Value
    Return Me
End Sub

Public Sub Total As Int
    Return mValue
End Sub

Edit: It is hard to give an answer faster than Erel. :)

Perfect.

Of course, this is not have practical effect but I'm was glad to know that it's possible to do this with b4x.
 
Upvote 0
Top