A Andrew King Member Licensed User Nov 11, 2024 #1 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: Copy 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: Nov 11, 2024
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: Copy 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?
A Andrew King Member Licensed User Nov 12, 2024 #21 aeric said: 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. Click to expand... 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 said: 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. Click to expand... I'm unsure how to create a new instance with a passed type without using GetType() and javaobject.initializeNewInstance. Is there a better way?
aeric Expert Licensed User Longtime User Nov 12, 2024 #22 Andrew King said: I'm unsure how to create a new instance with a passed type without using GetType() and javaobject.initializeNewInstance. Is there a better way? Click to expand... I only know by using GetType. I don't know how you can create a instance using javaobject.initializeNewInstance without using another method to contruct an instance. Upvote 0
Andrew King said: I'm unsure how to create a new instance with a passed type without using GetType() and javaobject.initializeNewInstance. Is there a better way? Click to expand... I only know by using GetType. I don't know how you can create a instance using javaobject.initializeNewInstance without using another method to contruct an instance.
aeric Expert Licensed User Longtime User Nov 12, 2024 #23 Here is my example. B4X: Copy 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: Copy 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: Copy ShowTheObject(CreateInstance("b4a.example.main$_fruit", Array("Apple", "Green"))) ShowTheObject(CreateInstance("b4a.example.main$_person", Array("Aeric"))) Upvote 0
Here is my example. B4X: Copy 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: Copy 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: Copy ShowTheObject(CreateInstance("b4a.example.main$_fruit", Array("Apple", "Green"))) ShowTheObject(CreateInstance("b4a.example.main$_person", Array("Aeric")))
A Andrew King Member Licensed User Nov 12, 2024 #24 aeric said: Here is my example. B4X: Copy 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: Copy 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: Copy ShowTheObject(CreateInstance("b4a.example.main$_fruit", Array("Apple", "Green"))) ShowTheObject(CreateInstance("b4a.example.main$_person", Array("Aeric"))) Click to expand... 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 said: Here is my example. B4X: Copy 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: Copy 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: Copy ShowTheObject(CreateInstance("b4a.example.main$_fruit", Array("Apple", "Green"))) ShowTheObject(CreateInstance("b4a.example.main$_person", Array("Aeric"))) Click to expand... 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
aeric Expert Licensed User Longtime User Nov 12, 2024 #25 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
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.
A Andrew King Member Licensed User Nov 12, 2024 #26 aeric said: 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. Click to expand... Well actually in my case I don't need the parameters. So I'm lucky for now. Upvote 0
aeric said: 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. Click to expand... Well actually in my case I don't need the parameters. So I'm lucky for now.