B4J Question [BANano] Is it possible to get a reference to a Class by name/variable?

alwaysbusy

Expert
Licensed User
Longtime User
if SomeClass is defined as a Global property, then you can do it with BANano.GetP("varname"):

B4X:
Sub Class_Globals
      Dim X As SomeClass
End Sub
...
Dim theClass As SomeClass = BANano.GetP("X")

' other stuff
BANano.SetP(theClass, "SomeGlobalProperty", "NewValue")

Dim theClassObj As BANanoObject = theClass 'ignore
theClassObj.RunMethod("SomeMethod".ToLowerCase, Array("SomeParamValue"))

For local variables within Methods, this is not possible.

BTW: Thank you for the donation!

Alwaysbusy
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Sure, pleasure...

I've been trying your code, just ran into a hurdle. Thanks a lot for this..

1. I can see the object that I want to call when logging the DesignerCreateView code. My B4J class is called ViewHome.

1632427995572.png


So I can access the _home object from there, so I tried

B4X:
Dim sBindStateComponent as string = "home" 'this receivesthe component name which can be dynamic from property bag)
Dim test As VueComponent = BANano.GetP(mCallBack, sBindStateComponent)

This got translated to

B4X:
// [364]  Dim test As VueComponent = BANano.GetP(mCallBack, sBindStateComponent)
_test = _B._mcallback['_' + _b._sbindstatecomponent];

resulting in an error

1632428574831.png


After I changed the small _b on the debug code, it works.

From

B4X:
// [364]  Dim test As VueComponent = BANano.GetP(mCallBack, sBindStateComponent)
_test=_B._mcallback['_'+_b._sbindstatecomponent];

To

B4X:
// [364]  Dim test As VueComponent = BANano.GetP(mCallBack, sBindStateComponent)
_test=_B._mcallback['_'+_B._sbindstatecomponent];

I saw on the code before where I check if the component is specified that its using a bigger B.

B4X:
// [354]  If sBindStateComponent = {117} Then
if (_B._sbindstatecomponent=="") {
// [355]  BANano.Throw( {22} )
throw "The component name should be specified for BindState type 'oncomponent'";
// [356]  Return
return;
// [357]  End If
}

So it seems this BANano.GetP will be a life saver for me! My. This right here saves the day and a lot of code lines, as soon as the _b (_B) issue is solved.

My.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
This one took some time to figure out as the snippet was a bit incomplete. The snippet did not state that 'Dim sBindStateComponent as string = "home"' was defined in Global_Class and not locally just before doing the .GetP() call. ;)

However, by putting sBindStateComponent in a temporary variable just before making the .GetP() call, you can avoid the _B thingy. All Global vars will have the _B prefix (as they have to be accessible from outside the class), local dimmed vars do not have this as their scope is inside the method only.

So we can do:
B4X:
' all local in some method
Dim tmpBindState as String = sBindStateComponent
Dim test As VueComponent = BANano.GetP(mCallBack, tmpBindState)
Log(test)

Alwaysbusy
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Eish... i marked the question SOLVED too soon.

I applied the solution inside DesignerCreateView

B4X:
[ERROR 102]: [VBtnIcon,designercreateview, around line: 0] The second parameter in GetP can NOT be a variable but must be a literal string!

I guess we were almost there. The _b/_B (bandage) thing worked without hurdles. This just knocks me off the horse now.

Please help. I really really need this.

And thanks again. ;)
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I was actually surprised that it did work as, indeed, it needs to be a literal string (Debug mode should've given this error too). This is a weird construction in itself because JavaScript variables do not have a type and .GetP() was not intended to be used like you want to use it. But as it does appear to work in Debug mode, I'll see if I can get it working also in Release Mode for the next version.

Alwaysbusy
 
Upvote 0
Top