Acknowledged

6.2.0 Cannot call function in Class - worked flawlessly with SDK 4.2.4

Cannot call function    curRadarBattery = myBikeRadarListener.getMyBattery();   

Gives an error:
ERROR: edge1040: C:\Garmin_IQ_Projekte\RadarBattery\source\RadarBatteryView.mc:53,8: Cannot find symbol ':getMyBattery' on class definition '$.myBikeRadarListener'.
Worked flawlessly with SDK 4.2.4  !

import Toybox.Activity;
import Toybox.Lang;
import Toybox.Time;
import Toybox.WatchUi;
import Toybox.AntPlus;


var myRadarBattery = 0;
class myBikeRadarListener extends AntPlus.BikeRadarListener {

    function initialize() {
        AntPlus.BikeRadarListener.initialize();
    }

    function onBatteryStatusUpdate(data) {
        if ( data != null ) {
            if ( data.batteryStatus != null ) {
                myRadarBattery = data.batteryStatus; 
            }
        } else {    
            myRadarBattery = 0;            
        }
    }

    function getMyBattery() {
        return myRadarBattery;
    }    

}


class RadarBatteryView extends WatchUi.SimpleDataField {

    var radarlistener;
    var bikeRadar;
    var curRadarBattery = 0;
    var radarBatStatus = "unknown";


    // Set the label of the data field here.
    function initialize() {
        SimpleDataField.initialize();

        label = "Radar Battery";

        radarlistener = new myBikeRadarListener();
        bikeRadar = new AntPlus.BikeRadar(radarlistener);

    }

    function compute(info as Activity.Info) as Numeric or Duration or String or Null {

        curRadarBattery = myBikeRadarListener.getMyBattery();    
 
        //for testing:
        //curRadarBattery = 3;

        if ( curRadarBattery == 0 ) {
            radarBatStatus = "unknown";
        } else if ( curRadarBattery == 1 ) {
            radarBatStatus = "new";             // 100%
        } else if ( curRadarBattery == 2 ) {
            radarBatStatus = "good";             // 75%
        } else if ( curRadarBattery == 3 ) {
            radarBatStatus = "okay";             // 50%
        } else if ( curRadarBattery == 4 ) {
            radarBatStatus = "low";             // 30%
        } else if ( curRadarBattery == 5 ) {
            radarBatStatus = "critcal";             // 10%
        } else if ( curRadarBattery == 7 ) {
            radarBatStatus = "invalid";           
        } else if ( curRadarBattery == 8 ) {
            radarBatStatus = "cnt?";
        }

        return radarBatStatus;
    }

}

Parents
  • As far as I can tell, that code should never have compiled or run.

    class myBikeRadarListener extends AntPlus.BikeRadarListener {
    
        function getMyBattery() {
            // ...
        }    
    }
    
    
    class RadarBatteryView extends WatchUi.SimpleDataField {
    
        var radarlistener;
    
        function initialize() {
            // ...
            radarlistener = new myBikeRadarListener();
            // ...
        }
    
        function compute(info as Activity.Info) as Numeric or Duration or String or Null {
    
            // ...
            curRadarBattery = myBikeRadarListener.getMyBattery();    
            // ...
        }
    }

    myBikeRadarListener is a type, and you are calling a non-static function on that type. You should only be calling non-static functions on instances. The call should be..

    curRadarBattery = radarListener.getMyBattery();

    I'd expect the type checker to call you out on that, but maybe you aren't using the type checker?

    - you have the same exact problem. I don't know what is going on in RefreshSettings, but if that function references any data in the firstscreen class, you need to pass an instance of firstscreen to the secondscreen somehow (either via a global, a constructor parameter, or a setter).

Comment
  • As far as I can tell, that code should never have compiled or run.

    class myBikeRadarListener extends AntPlus.BikeRadarListener {
    
        function getMyBattery() {
            // ...
        }    
    }
    
    
    class RadarBatteryView extends WatchUi.SimpleDataField {
    
        var radarlistener;
    
        function initialize() {
            // ...
            radarlistener = new myBikeRadarListener();
            // ...
        }
    
        function compute(info as Activity.Info) as Numeric or Duration or String or Null {
    
            // ...
            curRadarBattery = myBikeRadarListener.getMyBattery();    
            // ...
        }
    }

    myBikeRadarListener is a type, and you are calling a non-static function on that type. You should only be calling non-static functions on instances. The call should be..

    curRadarBattery = radarListener.getMyBattery();

    I'd expect the type checker to call you out on that, but maybe you aren't using the type checker?

    - you have the same exact problem. I don't know what is going on in RefreshSettings, but if that function references any data in the firstscreen class, you need to pass an instance of firstscreen to the secondscreen somehow (either via a global, a constructor parameter, or a setter).

Children