Classes in B4A?

boten

Active Member
Licensed User
Longtime User
Is there a way to define classes in B4A?

I mean define Type (structure) that will have some elements exposed (using some form of get/set or just object.property) while other elements are hidden to the user and are used to keep internal data.
 

Dextrosoft

Member
Licensed User
Longtime User
Hi,

Are there any plans to support classes in the long run ?
Or is this something that will be very unlikely due to b4a architecture?

Regards,
Wim
 
Upvote 0

jyounger

Member
Licensed User
Longtime User
Adding Classes is a GREAT idea!

Erel,

Take the case where I want to create a container class that has multiple views, say a checkbox, a button and a label. If I could define a class that contains those objects, and then "new" the class, then I could great a list or map of those object class instances. Methods are essential to classes in OO, so you should fully implement a class architecture with a "new" method, as well as accommodating public and private user defined methods and properties of the class. You can probably skip polymorphism and overloading for now, and just implement a simple class architecture.

I think your IDE is outstanding, and will be doing most of my Android development using it, but as it continues to garner adoption in the developer community, I believe you will ultimately have to address the level of sophistication required by the developer community.

Randy
 
Upvote 0

Philipp

Member
Licensed User
Longtime User
IMHO Classes would be a VERY good idea. Nowadays one is always taught oop. I tried to have a friend of mine (a professional programmer ) take a look at b4a, but having no classes he dismissed it immediately.

Besides that and besides the fact that b4a has types and through the activities you already have some kind of 'encapsulation' it would certainly be very helpful when doing bigger aps and certainly when doing games.
 
Upvote 0

naruto

Member
Licensed User
Longtime User
Classes

I would love the idea of classes. Coming from c#, c++, I would really appreciate being able to inherit, having base classes, etc. Seems to me its something b4a can't do without in the future.

By the way, I have the impression that in order for schools to adapt b4a, full object oriented support would be required due to the fact of most of them teach java, c# to their students.

Keep up the good work.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Surely anyone who is that much of an advanced programmer that they require proper classes would find it trivial to create a custom library for each project containing the classes and methods that they require..?

Just a thought!

Martin.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Do you mean Martin being a java expert. I have much programming experience going back more than 30 years but cannot learn java like I have picked up the other languages sadly.

I tried eclipse but didn't get far.

Regards Ricky
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
LOL i'm no java expert.

Before i bought B4A i couldn't write much java - it's only in the past few months that i've learnt a bit more but still i'm very much a beginner.

What i meant was that a basic class can be constructed in java with minimal knowledge.
If you can create a skeletal class something like:

B4X:
package com.mypackagename;

import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@Author("My Name")
@ShortName("MyB4AClass")
@Version(1.0f)
public class MyB4AClass {
   int myValue1;
   int myValue2;
   
   public void Initialize(int value1, int value2){
      myValue1=value1;
      myValue2=value2;
   }
   
   public int GetSum(){
      return myValue1+myValue2;
   }
   
   public void SetValues(int value1, int value2){
      myValue1=value1;
      myValue2=value2;
   }
}

Then Google is your friend and as long as you have time and patience you will soon find code examples that show you how to do more advanced things.

Martin.
 
Upvote 0

Woinowski

Active Member
Licensed User
Longtime User
Simple class alternative

Currently, when I try to "think" in classes in B4A I do it the following way:
  • Define a Code module with (Foo)
  • Define a type of the same name
  • Use methods like "DoSomething (this as Foo, param as Object)
  • Call those Foo.DoSomething(myF, x)

A simple fake class would be to allow custom typed variables to use such methods.

Something like
Dim myF as Foo
myF.DoSomething(param)

and B4A could translate that internally to Foo.DoSomething(myF, x)

It's not yet really object oriented, but would allow to think object oriented at least. Would only need a little change to the compiler and to auto-completion.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Actually B4A architecture can support classes. The Type keyword is converted to a Java class without methods.

I haven't yet decided if it is a good idea or not to add classes to B4A.

The correct way to do something is ALWAYS to leave it up to the user.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Martin, what would be the xml file that b4a needs with the jar file? Or does Eclipse create one for us? I'd like to experiment with this a bit to see where I can get once I've finished my current project - the 7th incarnation of my taxi application I use.

Regards, Ricky
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
In order to emulate OO just a tiny bit, I do it like this:

Assume I have a code module called "MyClass":

B4X:
Sub Process_Globals
   Type ThisMyClass (privateVar1 as Int,  privateVar2 as Int, ...  )
End Sub
 
Sub Initialize As ThisMessage
    Dim result As ThisMyClass
    result.Initialize
    result.privateVar1 = 1
    result.privateVar2 = 2
    Return result
End Sub
 
Sub SomeMethod(this As ThisMyClass)
    this.privateVar1 = this.privateVar1 + 1
    ...
End Sub

But I do like the idea to be able to include at least methods in the Type, so at least you can encapsulate members and methods (adding polymorphism, inheritance, scope identitifiers would be even nicer of course ;-)
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Martin, what would be the xml file that b4a needs with the jar file? Or does Eclipse create one for us? I'd like to experiment with this a bit to see where I can get once I've finished my current project - the 7th incarnation of my taxi application I use.

Regards, Ricky

Take a look at the library tutorials, this one explains it: http://www.b4x.com/forum/libraries-developers-questions/6810-creating-libraries-basic4android.html.

You configure Eclipse to create the library XML file.

Martin.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Thanks Martin, I'll look at it more closely soon.

Regards, Ricky
 
Upvote 0
Top