Wish "Class" Methods

qsrtech

Active Member
Licensed User
Longtime User
Hopefully our "class" modules can be updated to include a Class keyword in front of Sub to allow class helper methods. For those that don't quite understand what a class method is, it's basically a method that can be called independant of a specific instance of the class. For example, technically the "Initialize" is a "class" method since it isn't actually an instance of the class calling it.

Technically this is how the code should look to create an instance of a class:
B4X:
Dim AInstance as CMyClass
AInstance=CMyClass.Initialize 'other languages might use the "New" keyword, i.e. AInstance=New CMyClass.Initialize

The Initialize sub would look like this:
B4X:
Class Sub Initialize()
'do something
End Sub

So essentially with a "Class" method, you would have some kind of helper routine you'd like to include within the class, but not necessarily require a specific instance of the class to call it

For example:
B4X:
'CMyClass

Class Sub GetANumber as int
    Return 100
End sub

'somewhere in your app

Dim x as int
x=CMyClass.GetANumber
 

qsrtech

Active Member
Licensed User
Longtime User
As I see it, the Initialize method is confusing compared to every other language that I'm aware of. The "instance" variable is not valid until the Initialize is called so it essentially is a "Class" method. Constantly adding code modules is not a very good solution for the long term maintenance and re-usability. Also, I'm not sure if this would work but it may be possible to use CallSub,etc on a class method vs code modules. I hope you're not going to restrict b4a's growth by keeping it watered down. The product has come a long way in my two years. Please keep the momentum going as the audience for this product will be tremendous once it's matured.
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User

qsrtech

Active Member
Licensed User
Longtime User
I like the way Erel has done the classes. Its easy for the beginner and yet can be pretty powerful when needed. I know static methods can be useful as well as static and abstract classes but then we are back to this discussion:
B4X:
http://www.b4x.com/android/forum/threads/object-oriented-programming.37001/

Thanks for your opinion. Did you like the classes before or after the "property" feature was added? If it was before then why bother adding the "property" feature? If it's after then why weren't you happy with the basic "class"?

And it's not about necessarily about being "powerful", it's about building clean, consistent, manageable code. It's easy to slap something together, the hard part is building on down the road or when a new developer takes over the project. It could also look like poor code design to outsiders.
 

qsrtech

Active Member
Licensed User
Longtime User
And just in case there is some confusion, I'm not suggesting to change the way "Initialize" is implemented/used. I merely used it as an example.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I see it, the Initialize method is confusing compared to every other language that I'm aware of. The "instance" variable is not valid until the Initialize is called so it essentially is a "Class" method. Constantly adding code modules is not a very good solution for the long term maintenance and re-usability. Also, I'm not sure if this would work but it may be possible to use CallSub,etc on a class method vs code modules. I hope you're not going to restrict b4a's growth by keeping it watered down. The product has come a long way in my two years. Please keep the momentum going as the audience for this product will be tremendous once it's matured.

I don't think that this suggestion will lead to any progress. The language is cleaner now. You have classes with their instance methods and you have static code modules. Combining them will only make the language more confusing / less elegant.

Initialize is not a static method. Static methods cannot access instance variables. Initialize is more similar to Java / C# constructors. Even if you do consider it a static method, it doesn't really matter for this suggested feature.

When designing programming languages it is tempting to add more and more features. However the more you add the more you make the language difficult to master. Each feature should be carefully considered. Once you add a feature it is there for good...

Also, I'm not sure if this would work but it may be possible to use CallSub
You will also not be able to use CallSub (or more importantly raise events) with "static class methods". The reason for this restriction is that code modules do not hold a context of their own. This will also be the case with static class methods. This means that it will even be more confusing.
Note that this restriction is related specifically to the way Android works. In B4J for example you can raise events or use CallSub with code modules.

You will also not be able to access the class global variables from these static methods as the global variables are instance variables (this is why Initialize for example cannot be a static method).

This is my view. I usually try to avoid these arguments as there is no one correct way and to each his own opinion. I will not participate in this discussion any further.
 
Last edited:
Top