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

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds