Android Question Dynamically change libraries

JackKirk

Well-Known Member
Licensed User
Longtime User
In B4A I have:

Public Obj_library As library01
Public Obj_library_g As library01g
Public Obj_library_t As library01t

where library01, library01g and library01t are libraries - all with the same methods.

I want to be able to change Obj_library at run time to either library01g or library01t

how do i do it?

I have tried:

Obj_library.As(library01g)

and

Obj_library = Obj_library_g

but neither works, ObJ_library retains its initial setting.
 

teddybear

Well-Known Member
Licensed User
I have tried:

Obj_library.As(library01g)

and

Obj_library = Obj_library_g

but neither works, ObJ_library retains its initial setting.
The Obj_library has not the As method, Obj_library and Obj_library_g are defferent classes.
if you want to change libraries dynamically, you can do that with object reference like this
B4X:
public obj as Object
Public Obj_library As library01
Public Obj_library_g As library01g
Public Obj_library_t As library01t

condition 1
    obj=Obj_library
condition 2
    obj=library01g
condition 3
    obj=library03g

 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
B4X:
public obj as Object
Public Obj_library As library01
Public Obj_library_g As library01g
Public Obj_library_t As library01t

condition 1
    obj=Obj_library
condition 2
    obj=library01g
condition 3
    obj=library03g
Unfortunately that is not working this end.

The "obj" is subsequently used in statements of the form:

wrk_map = collection_serializer.CompressedBytesToMap(Obj.xxgM)

which are blowing up in the log with:

Unknown type: Object
Are you missing a library reference?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
You'd better post code snippets or upload a small project. I don't quite understand what you want to do by your question,
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
In B4A I have:

Public Obj_library As library01
Public Obj_library_g As library01g
Public Obj_library_t As library01t

where library01, library01g and library01t are libraries - all with the same methods.

I want to be able to change Obj_library at run time to either library01g or library01t

how do i do it?

I have tried:

Obj_library.As(library01g)

and

Obj_library = Obj_library_g

but neither works, ObJ_library retains its initial setting.
It seems like polymorphism and inheritance. During runtime you have to make sure that all libraries should have and share common behaviors.
If the methods are identical and you're certain about the object's type at runtime, you can use a method to re-assign the library.


B4X:
Sub SwitchLibrary(newLibrary As Object)
    Obj_library = newLibrary
End Sub

'Call it like this:
SwitchLibrary(Obj_library_g)
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Sub SwitchLibrary(newLibrary As Object) Obj_library = newLibrary End Sub 'Call it like this: SwitchLibrary(Obj_library_g)
Same result as my initial post: ObJ_library retains its initial setting.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
wrk_map = collection_serializer.CompressedBytesToMap(Obj.As(libray01g).xxgM)
This defeats the purpose - by the time I get to the collection_serializer the library being serialized should be set to either library01g or library01t.

I have come to the conclusion that what I want to do this way is not possible - so I have come up with a small sub that just does one library or the other based on a pointer.

Thanks all for your inputs....
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I think it's possible (but not easy) to do, using a custom classloader from inline java.
 
Upvote 0
Top