Sub Process_Globals
Dim OO As JavaObject
Dim ccc As Int = 4444
End Sub
Sub Globals
Dim aaA1 As Int =5555
Dim AAa2 As Int =6666
Dim AaA3 As Int =7777
Dim zzz(3) As Int
End Sub
Sub Activity_Create(firsttime As Boolean)
OO.InitializeContext
'You should change the string below for <yourpackagename>.<main> (or the module name if not 'main')
OO=OO.InitializeNewInstance("jcp.b4a.thirdlib.main",Null)
'--------------------------------------------------
' Do this To get the value of a variable
'--------------------------------------------------
Log(OO.GetField("_aaa1")) 'Lowercase and with a leading "_"
'--------------------------------------------------
' Try to set a new value
'--------------------------------------------------
Dim newval As Int =1111
' Dim newval As String ="dddddd"
'OO.SetField("_aaa1",Array(val)) 'This doesn't work for me (crashes)
Dim res As Int = OO.RunMethod("SetClassValue",Array("_aaa1",newval)) ' 0:ok, 1:not such field, 2:could not assign value
Log(OO.GetField("_aaa1")) 'Both are the same. Should give 1111
Log(aaA1)
'------------------------
' Arrays
'------------------------
zzz(0)=10000:zzz(1)=20000:zzz(2)=30000
Log("Array values before:"& zzz(0)&","&zzz(1)&","&zzz(2))
For index=0 To 2
Dim CurrentVal As Int
'This function to get a value from an array
CurrentVal = OO.RunMethod("GetIntArrayValue",Array("_zzz",index))
'This one to modify it
Dim res As Int = OO.RunMethod("SetIntArrayValue",Array("_zzz",index,CurrentVal+1)) ' 0:ok, 1:not such field, 2:could not assign value
Next
Log("Array values after: "& zzz(0)&","&zzz(1)&","&zzz(2))
End Sub
#If java
import java.lang.reflect.Field;
public int SetClassValue(String str, int val){
Field aField;
try {
aField= getClass().getDeclaredField(str);
}
catch(Exception e){
return(-1); // Not such field
}
try {
aField.set(this, val);
}
catch(Exception e){
return(-2); // Could not assign value
}
return(0);
}
public int GetIntArrayValue( String str, int index ){
int a[];
try{
a = (int[]) getClass().getField(str).get(null);
}
catch(Exception e){
return(-1); // Not such field
}
return (a[index]);
}
public int SetIntArrayValue( String str, int index, int value ){
int a[];
try{
a = (int[])getClass().getField(str).get(null);
}
catch(Exception e){
return(-1); // Not such field
}
try {
a[index]=value;
}
catch(Exception e){
return(-2); // Could not assign value
}
return(0);
}
#End If