Creating Flexible Constant Fields

It is very easy to add constant fields to your classes. All you need to do is add the const keyword to the declaration and set the value. You now have a field whose value cannot change during the execution of your program. Here is an example of using const:

private const double _kP = .5; 
private const double _kI = .3; 
private const double _kD = .2; 

This example comes from a class to manage a PID control loop. I learned about PID control when working with the local robotics team. PID controls allow the robot program to maintain a constant speed or to turn a turret to a set position. This is accomplished by using information about how far away from the target value you are and how fast you are approaching the target value are used to adjust the output value.

Three constants are used to determine how to adjust the output value. These constants will be different for each application of PID control. The constant values for maintaining a constant speed will be different than the constants used to turn the turret to a set position. If you use the const keyword for these fields, you will have to create a separate class for each application of PID control so that you can adjust the constants for each situation. Using this method, the number of classes could grow very quickly and become hard to manage.

There is an alternative, however, that allows you to set the constant values once and ensure that they will not change for the rest of the life of the object. The solution is to use the readonly keyword instead of the const keyword. Readonly fields are declared like const fields and may include an initializer. Declaring the above fields using readonly looks like this:

private readonly double _kP; 
private readonly double _kI; 
private readonly double _kD; 

The difference between const and readonly is that readonly fields can be initialized when declared and their values can be set in a constructor. This allows you to set the values for the constants when the object is created and ensures that the values of the constants cannot change. This way you can use a single class to manage PID control and simply set the constants when each new instance is created. The constructor for the PID control class might look like this:

public PIDControl(double kP, double kI, double kD) 
{ 
    _kP = kP; 
    _kI = kI; 
    _kD = kD; 
}  

Attempting to set the value of a readonly field outside of a constructor is a compile-time error. Readonly fields give you the flexibility to set their value in the constructor and prevent them from changing later. Your PID control variables can then be created like this:

PIDControl TurretControl = new PIDControl(.5, .3, .2); 
PIDControl SpeedControl = new PIDControl(.8, .4, 0); 

You can find more information about PID control at http://en.wikipedia.org/wiki/PID_controller.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds