Shop OBEX P1 Docs P2 Docs Learn Events
pass on value — Parallax Forums

pass on value

PlaneTeeRPlaneTeeR Posts: 100
edited 2006-05-31 10:27 in General Discussion
how can i pass on the value between the classes, this is what i used:

I wanted the processor to calculate the speed with calculateSpeed() and give it to display with getSpeed().

In display i placed a system print line but it didn't print, what should i do? or what did i do wrong?

Johnny

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-30 20:33
    There are two ways to pass values to classes:
    as arguments to methods, but then only those methods know the value
    or as arguments to constructors, that store it locally.
    The stored value is then known to all the methods of that class.

    For example in classI:

    classII myClassII = new classII(someValue); //pass someValue to classII


    In classII:

    public class classII {

    · int value;

    · //constructor
    · public classII(int value) {
    ··· this.value = value; //store the passed value
    · }

    · public void someMethod(int value) {
    ··· return this.value + value; //some calculation with the stored value and the value argument
    · }
    }

    In case of identical names, use the this. prefix to access the locally stored class variables.

    regards peter

    Post Edited (Peter Verkaik) : 5/30/2006 8:44:05 PM GMT
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-31 09:50
    Oke, so when i program this:

    Processor:

    ...
    sen = new Sensor();
    ...
    public void calculateSpeed(){
    · spd = UnsignedIntMath.umulf((short)36000,UnsignedIntMath.ufrac(circumference,sen.getPulseTime()));
    }


    Sensor:

    · private int pulseTime;

    · public int getPulseTime() {
    ··· return pulseTime;
    · }

    It must work?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-31 10:27
    That looks ok.

    regards peter
Sign In or Register to comment.