Android Question [Solved] Sub as function without return?

Pflichtfeld

Active Member
Licensed User
In the nice Erels Orientation Tutorial one can find this sub:

B4X:
Sub AddSensor(SensorType As Int, Name As String, ThreeValues As Boolean) As SensorData
    Dim sd As SensorData
    sd.Initialize
    sd.Name = Name
    sd.ThreeValues = ThreeValues
    Dim ps As PhoneSensors
    ps.Initialize(SensorType)
    SensorsMap.Put(ps, sd)
    Log(Name & " MaxValue = " & ps.MaxValue)
End Sub

This sub is envoked:
B4X:
Dim ps As PhoneSensors 'This object is only used to access the type constants.
        AddSensor(ps.TYPE_ACCELEROMETER, "ACCELEROMETER", True)
        AddSensor(ps.TYPE_GYROSCOPE, "GYROSCOPE", True)
        AddSensor(ps.TYPE_LIGHT, "LIGHT", False)
        AddSensor(ps.TYPE_MAGNETIC_FIELD, "MAGNETIC", True)
        AddSensor(ps.TYPE_ORIENTATION, "ORIENTATION", True)
        AddSensor(ps.TYPE_PRESSURE, "PRESSURE", False)
        AddSensor(ps.TYPE_PROXIMITY, "PROXIMITY", False)
        AddSensor(ps.TYPE_TEMPERATURE, "TEMPERATURE", False)

1. The IDE tells me: "nicht alle Codepfade geben einen Wert zurück" (Not each line returns a value), which refers to this sub
2. The warning vanishes, when I delete the "As SensorData" in the Line: Sub AddSensor...
3. If I do 2., the compiler throws an error:
Error occurred on line: 56 (Main)
java.lang.ClassCastException: java.lang.String cannot be cast to anywheresoftware.b4a.samples.sensors.main$_sensordata
at anywheresoftware.b4a.samples.sensors.main._addsensor(main.java:470)
at anywheresoftware.b4a.samples.sensors.main._activity_create(main.java:407)
...
where line 56 is the line with "End Sub". Why this error?
4. The sub does not contain a return <variable>. So what is returned?
5. And why is it necessary, that it has a return-value?
 

OliverA

Expert
Licensed User
Longtime User
The sub declares a return type (As SensorData), but has no return of that type of data in the sub. Without explicitly using Return [with some optional return value], the sub still does a return and it looks like the default return type is a string. Looking at the code sample above, it looks like it is a bug that the sub has the As SensorData return type declared. Take that off and everything should work as expected (as long as the rest of the code is ok)
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
Thanks Oliver,
you are right: If I change the return type to a string it also works.
But if I take the return type away, the above described error is cast.
So: why is it obligatory, to set a/any returntype?
And what actually is returned? Returned to what?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You don't need to use a return type. You only need it if you actually return something. If you do use a return type, you must return that type (or you get the errors you mentioned)
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
Sure you're using the above code as is?
No, you are right. Changed the minSDKVersion in Manifest from 4 to 16 because the IDE tells me,
"Der empfohlene Wert für android:targetSdkVersion ist 28 oder höher (Manifesteditor). (warning #31)" (should be: the recommended value for android:targetSdKVersion is 28 or higher) and according to Erels recommendation to use ad least Version 16.
I cannot change to Version 28 because my last installed version is 27 as I am told during installation:
Installiere Datei auf Gerät. Error
adb: failed to install Sensors_RAPID_DEBUG.apk: Failure [INSTALL_FAILED_OLDER_SDK: Failed parse during installPackageLI: /data/app/vmdl1866911847.tmp/base.apk (at Binary XML file line #9): Requires newer sdk version #28 (current version is #27)]



You don't need to use a return type.
As I told you, Oliver. Omitting the returntype in this project leads to error during compilation
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Yes, it is the zipped project from erel from above mentioned page.


As I told you, Oliver. Omitting the returntype in this project leads to error during compilation
I get no error using this project when I omit the return type of the sub.
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
Sure you're using the above code as is?
I downloaded the project again, started it without changes, but now I get an Install-Error:
Installiere Datei auf Gerät. Error
adb: failed to install Sensors_RAPID_DEBUG.apk: Failure [-26: Package anywheresoftware.b4a.samples.sensors new target SDK 4 doesn't support runtime permissions but the old target SDK 26 doe
s.]
 
Upvote 0
Top