Android Question Question about B4X Class

Frankie Lagrange

Member
Licensed User
I’ve been learning about B4X classes. And I find them akward to use, so here a few questions that would help me clarify my thoughts?

How do you extend a class?

How do you declare multiple constructors/initializers?

So far, I understand that B4X classes implement only two concepts: Encapsulation and Abstraction but NOT Inheritance and Polymorphism.

Am I right?

If that were the case, the B4X "Classes" are more akin to modern Structures which define properties and methods, than real Classes.
 

LucaMs

Expert
Licensed User
Longtime User
How do you declare multiple constructors/initializers?
I think you can use one of (at least) three workarounds:
B4X:
Public Sub Initialize(ConstructorName As String, Params As Map) ' <--- or Params() As Object
    CallSubDelayed2(Me, ConstructorName, Params)
End Sub

Private Sub MySecondInitialize(Params As Map) ' <--- I passed "MySecondInitialize" as ConstructorName, of course
'...
End Sub

Private Sub MyThirdInitialize(Params As Map) ' <--- I passed "MyThirdInitialize" as ConstructorName, of course
'...
End Sub

Or:

B4X:
Public Sub Initialize(ConstructorID As Int, Params As Map) ' <--- or Params() As Object
    Select ConstructorID
        Case 1 ' or constants, of course
            CallSubDelayed2(Me, "Initialize1", Params)
        Case 2 ' or constants, of course
            CallSubDelayed2(Me, "Initialize2", Params)
       ' ...
    End Select
End Sub

Private Sub Initialize1(Params As Map)
'...
End Sub

Or:

B4X:
Public Sub Initialize(Params As Map) ' <--- or Params() As Object
    CallSubDelayed2(Me, "Initialize2", Params)
End Sub

Private Sub Initialize2(Params As Map)
    Select Case Params.Size
' or If Params.ContainsKey("MyInitialize1") Then
' ...
End Sub


So far, I understand that B4X classes implement only two concepts: Encapsulation and Abstraction but NOT Inheritance and Polymorphism.
https://www.b4x.com/android/forum/t...amming-to-b4x-inheritance-polymorphism.64453/
(mainly post #7)



If that were the case, the B4X "Classes" are more akin to modern Structures which define properties and methods, than real Classes.
Probably. Anyway, I don't like "modern Structures"; you should have only "classic Structures" (custom types) and classes; but... I am old ?
 
Last edited:
Upvote 0

Frankie Lagrange

Member
Licensed User
I studied Luca's examples. Though I would prefer something using overloading, like in the code below, we could live with workarounds.

B4X:
  Initialize(firstName as String, lastName as String)
  Initialize(firstName as String, lastName as String, dob as Date)
etc...

However, the lack of proper inheritance (is-a relationship) is a show-stopper for us. Compositon (has-a relationship) is OK and even perfect in some scenarios, but not as a systematic mechanism for code reuse, as it's not always appropriate and often not as efficient or elegant, unless one can combine Composition with Interfaces, which unfortunately are not supported by B4X.

That'll be the end of my evaluation of B4X as it's a showstopper for us.
 
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
Instantiatinge an object before I start to use seems fine to me, but I'm just simply folk
 
Upvote 0

j_o_h_n

Active Member
Licensed User
I must say, inheritance is the one feature I miss the most in B4X. That is just such a handy tool to have in your bag.
I liked your comment but I didn't mean to, I meant to click Reply! (I'm neutral on the subject)
I just wanted to say that it is interesting that Golang also does not have inheritance and it is quite a new language.
 
Upvote 0

Frankie Lagrange

Member
Licensed User
j_o_h_n, to remove a Like, click on the Like button again.
I just wanted to say that it is interesting that Golang also does not have inheritance and it is quite a new language.


True, but as I mentioned in my post (Inheritancs vs Combination and Interfaces), Go combines Composition and Interfaces, and has the functionnal equivalent of Class with Embedding. Plus Go offers default parameters, overloading etc. We (as in our company) have been evaluating Go (and gomobile), but for multiplatform RAD development, B4X is well ahead.

B4X is very easy to pick up, as a language and concept, and its Developers have been doing a great job in building multiplatform UI and libraries. I think they should accept contributions so that the platform evolves faster.

B4X has short-comings and strengths, like any other product, but I cannot recommend my company to endorse it because of that glaring omission.

I’m not saying that it’s a show-stopper for everybody as developers wrote code before OO was a mainstream concept (I was a C developer for 15 years before C++ came to the scene), but I’m saying it is not for us and the way we work. There are some other tools we’re still in the process of evaluating.

That’ll be my very last post on this forum as I don’t want to be seen as sneering at B4X. It’s an excellent tool with a nice community (thanks again Luca), but it’s not for us. That’s all.

Good luck to you all.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
as I don’t want to be seen as sneering at B4X
I don't think anyone was thinking that. It is a valid consideration you are making to start working or not with a product. I've dismissed recommending other tools too in the past (for other reasons) because they didn't fit with our company programming philosophy. Lucky for us (and me), B4X fits like a glove. Especially B4X's track record on stability and bug fixing is 'out-of-this-world'! This minor glitch of the missing 'inheritance' for us does not outweigh the many advantages/unique features it has over all other tools. The 'Wait For' feature, the 'live' code debugging, the library system(s), this forum(!) are only a couple of features that make B4X development speed many times faster than any other tool.

Other cross-platform tools out there exist for over 2 decades and can't even reach the heels of B4X. On production speed, stability, features and support there isn't a tool out there that can match B4X.

In short, unless the 'missing' things are absolutely necessary, take your time to further evaluate B4X. You will quickly notice what all those other tools are missing.

Alwaysbusy
 
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
A few years back I overlooked B4X for the simple fact it lacked one thing I considered a must have for a project we were undertaking. Biggest mistake of my life. The project still chugs away costing us over $US 2k in licenses per annum. Sure the customer covers the cost plus some, but that's not the point. If we want support we pay for every ticket. If we want training, we pay for it. The learning curve was over the top. I haven't got a single nice thing to say about it. One day I will get around to replacing it written in B4X, in the meantime we have moved on to bigger and better things with unparalleled support from this forum.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…