{wish} New Classes Additions

EduardoElias

Well-Known Member
Licensed User
Longtime User
I am new to B4A, got it on 2.0 and without classes it was going to be hard for me.

it is a good addition. I do understand that the nature of B4A is to be simple as possible, but I see that both simple and advanced could co-exist. For more advances bussines apps classes more elaborated are necessary. Hard to mantain the code without that.

Suggestions:

1 - Get and Set Properties

Class.Value is the member visible
SetValue and GetValue would be the real subs. It could be Automatic Named in the code we could declare one line only

Public Property Value As String Set "SetValue" Get "GetValue"

Sub SetValue(x as string)
end sub

Sub GetValue as string
End sub


2 - Basic Inheritance
That will open a door for expanding even more the classes created. It will be really useful if it permit inherit a class from a library. In this case the comunity created libraries could be expanded with no need to change the original.

example:

a new reserved word could be created at beginning of the class file:

Inherited <class module>

Sub Process_Globals
...

Then simple inheritance could be implemented based on the sub signatures. A new word "inherited" could be used in the sub context as a implicit "callsub" to the parents sub with the same signature

The subs from the parent's class that does not match with any sub will be exposed as part of the new created class. Eventually to make easy to implement at compile time the missing subs from the parents could be created as virtual subs with implicit call to the parent.

3 - an great addition would be sub overriding.
example:

SaveLog(x as string)
SaveLog(x as map)

since the 2 subs have different signature it would be different, the compile will choose according to the parameter passed.


4 - Enumaration:
example:

TDataSetState = (
dsInactive,
dsBrowse,
dsEdit,
dsInsert,
dsSetKey,
dsCalcFields,
dsFilter,
dsNewValue,
dsOldValue,
dsCurValue,
dsBlockRead,
dsInternalCalc,
dsOpening
);

it is a much better way than using variables as constants. That is internally mapped as int and each enumaration has a value starting from 0


These would be really great and valuable additions to me, I am giving it based on my backgroud experience, probably in visual basic way there are more compatible style. I am writing bussines applications that needs to have a lot of reused code. Compile to Library saved my life, but there are some situations that still hard to me.

Polymorphism is something needed, and Inheritance solves this situation. I am using the example of Polymorphism that is explained, but that is too simple, when it is needed that the parent class really introduces behavior and holds data, and the children expand that. For example:

Class DataSet

Class MemoryTable Inherit DataSet

Class SQLTable Inherit DataSet


The DataSet class generally defines the class interface, not implementing all the members (virtual subs) but some Subs are declared with functionality.
Children classes expand it, or commonly implement the virtual subs from the parent.

Then using the DataSet makes transparent who is the real children object.

I am certain that I am explaining the obvious...... sorry, got enthusiastic...

Eduardo
 

bartv

Member
Licensed User
Longtime User
In favor

  • Inheritance/Interfacing/Overriding would certainly help a great deal in structured class design.
  • Can we create classes with indexers?
  • Overloading methods seems to be a big problem for B4A, as the APIs are all having xyz() and xyz2() methods to avoid this. Frankly, I'm having some trouble understanding why this isn't implemented from the start, since the base is Java, and it is already there...

Greetings,
Bart uit Mechelen
 

Vader

Well-Known Member
Licensed User
Longtime User
  • Inheritance/Interfacing/Overriding would certainly help a great deal in structured class design.
  • Can we create classes with indexers?
  • Overloading methods seems to be a big problem for B4A, as the APIs are all having xyz() and xyz2() methods to avoid this. Frankly, I'm having some trouble understanding why this isn't implemented from the start, since the base is Java, and it is already there...

Greetings,
Bart uit Mechelen

wish++ ;)
 

aalekizoglou

Member
Licensed User
Longtime User
Yes,

coming from 15 years Delphi, 4 years PHP, I wish too:

getters/setters
multi-classes in a single file
overriding
inheritance

:)
 

gawie007

Member
Licensed User
Longtime User
I have just been trying to learn the Command Design Pattern in order to improve my programs menu structures.
This however requires that the class should have an Interface and other classes implementing the interface.
Is there any other way to do this and will it be available in B4A?
Design Patterns seem to simplify proven coding methods but seem to be based on some of the other (unavailable in B4A) features of classes from what I can make out.
I may be wrong since I have only bought a couple of books on Design Patterns and so far most of it has gone right over my head - hence the desire to learn by practice.
 

gawie007

Member
Licensed User
Longtime User
You cannot define an interface in Basic4android. However you can use CallSub (and SubExists) to implement a similar solution. This is named "duck interface".
Hi Erel,
Thank you for your reply.
I have tried to get an understanding of Duck Typing looking at your example in the class tutorial.
Additionally, I have tried to convert the code from http://en.wikipedia.org/wiki/Talk:Duck_typing#Visual_Basic to use in B4A. I have attached the modified code. This is also to try to create a clear example without getting confused by the lists etc.
I have commented out the following code (in the main module)since it does not accept the difference in types and once I can get it to work.... I think this will be the crux of me understanding how all this works.


B4X:
'    For JJ = 0 To 4
'        DuckWorld(john)
'    Next

One day the penny will drop!
 

Attachments

  • DuckTypeTest.zip
    6.9 KB · Views: 216

gawie007

Member
Licensed User
Longtime User
You cannot cast a "person" to a "duck". You should use CallSub:
B4X:
Private Sub DuckWorld( aDuck As Object) 'Object, not Duch
  If CallSub(aDuck, "inFlight") Then
  CallSub(aDuck,"defecate")
  Else
  CallSub(aDuck, "quack")
  End If
End Sub
Thank you very much Erel!
Now I will try to create a real world example.
 

gawie007

Member
Licensed User
Longtime User
Thank you very much Erel!
Now I will try to create a real world example.
Hi Erel,
If I have got this example right, then everyone can use it.
It shows a different example of Duck Typing as well as a basic Command Pattern.

Some uses of the Command Pattern are:
  • Menu's - that can be shuffled around dynamically - the Duck Typing will "find" the right one.
  • Queuing requests - i.e. list of jobs: once one is completed select the next in the queue.
  • Macro Commands.
  • Logging requests.

Command Pattern Definition:
The Command Pattern encapsulates a request as an object, thereby letting one parametise
other objects with different requests, queue or log requests, and support undoable operations


The attached program in layman's terms:
The user interface is a remote control for an automated house
The user clicks any button on the remote and sends an encapsulated message. The Command Object
doesn't care what it is or where it goes - it is just an object.
The Receiver (Class) waits for an action to be invoked upon it.​
 

Attachments

  • DuckTypingCommandPattern.zip
    10.2 KB · Views: 217
Last edited:
Top