Question

I am having a problem when sending my track bar problems. I am trying to have my program so that when I run the procedure updateValues it sends a decimal value to some double variables I have in another class "Pendulum".

My logic is this:

The trackbars only use integer values so to increment in decimals certain values have to be divided by 100 so they are correct in the simulation.
For example if I want the gravity to be 0.4 my track bars value will have to be 40 so 40 / 100 = 0.40
Another example will be angle that will go from -3 to 3 incrementing in decimals. So we go from -300 to 300 /100 = 3.

So surely the code solution for this is:

 Pendulum.angle = (tbrAngle.Value / 100); 

After putting a breakpoint in I found that with my trackbar value at 161 the pendulum angle only becomes 1.0.

So what exactly is going on? Here is the whole method with the values in context (just for reference):

Inside frmPendulm Class:Form

private void UpdateValues()
                {

                Pendulum.length = tbrLength.Value; //length is ok as it is an integer
                Pendulum.angle = (tbrAngle.Value / 100);  //Not working.
                //acceleration is calculated so isn't sent
                Pendulum.aVel = tbrAVel.Value; //must convert
                Pendulum.damping = tbrDamp.Value; //must convert
                Pendulum.gravity = tbrGrav.Value; //must convert
               //This method is run on a button click.
            }

The variables in my class Pendulum:

//all public static so they are actually global (can be used between classes and not just global to this class).
        public static int length = 10;//length of arm /** can't be less than 0 or will break.
        public static double angle = 0; //pendulums arm angle
        public static double aAcc = 0.00; //Angle acceleration - calculated
        public static double aVel = 0.00; //anglular velocity
        public static double damping = 0.000; //friction
        public static double gravity = 0.0; //make gravity a constant

Solution In order to convert to a double both the numerator and denominator have to be of the same type. In this case a double.

Was it helpful?

Solution

How about explicit conversion?

Pendulum.angle = ((double)tbrAngle.Value) / 100.0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top