Android Question Create Object Instance of B4A Object Using The Typename

Andrew King

Member
Licensed User
I am trying to find a way to create an instance of any type of object using the typename.

I need to create objects at runtime. I know this is kind of frowned upon but in some cases when writing classes to be used in other projects, this function is needed.

To be used like this:

B4X:
Dim obj as object
obj = CreateInstance("b4a.example.fruit")

This is a very simple example that doesn't make much sense to any application but it demonstrates how it could be used.

Is there a function or existing library that can do this and if not what would be the best way to accomplish this?
 
Last edited:

Andrew King

Member
Licensed User
edit: How do you know the parameters for the constructor? ok, it is possible.
I don't think you need to use JavaObject in this case.
I'm unsure how to create a new instance with a passed type without using GetType() and javaobject.initializeNewInstance. Is there a better way?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is my example.
B4X:
Public Sub CreateInstance (TypeName As String, Params() As Object) As Object
    Select TypeName
        Case "b4a.example.main$_person"
            Dim t1 As Person
            t1.Initialize
            If Params.Length > 0 Then t1.FirstName = Params(0)
            If Params.Length > 1 Then t1.LastName = Params(1)
            Return t1
        Case "b4a.example.main$_fruit"
            Dim t2 As Fruit
            t2.Initialize
            If Params.Length > 0 Then t2.Name = Params(0)
            If Params.Length > 1 Then t2.Color = Params(1)
            Return t2
        Case Else
            Return Null
    End Select
End Sub

B4X:
Sub ShowTheObject (obj As Object)
    Select GetType(obj)
        Case "b4a.example.main$_person"
            Log("The person first name is " & obj.As(Person).FirstName)
        Case "b4a.example.main$_fruit"
            Log("The fruit name is " & obj.As(Fruit).Name)
    End Select
End Sub

Usage:
B4X:
ShowTheObject(CreateInstance("b4a.example.main$_fruit", Array("Apple", "Green")))
ShowTheObject(CreateInstance("b4a.example.main$_person", Array("Aeric")))
 
Upvote 0

Andrew King

Member
Licensed User
Right but in my case, I am using this function in a library that will be used in other applications so the types won't be known in the createinstance function
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is my limit. You need an advice from Erel on this.

I don't think currently B4X has automatic constructors or polymorphism that know which parameter maps to which property.
I also don't think you can use JavaObject to call B4X custom type or class.
Lastly, I won't create a complex library like this. Sounds universally flexible but I prefer more user friendly method name for each class.
 
Upvote 0

Andrew King

Member
Licensed User
Well actually in my case I don't need the parameters. So I'm lucky for now.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…