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

  • Data integrity and ultra-high performance dictate the success and growth of many companies. One of these companies is BridgePay Network Solutions, a recently launched and rapidly growing financial services organization that allows merchants around the world to process millions of daily credit card transactions. Due to the nature of their business, their IT team needed to strike the perfect balance between meeting regulatory-mandated data security measures with the lowest possible levels of latency and …

  • The 2014 State of DevOps Report — based on a survey of 9,200+ people in IT operations, software development and technology management roles in 110 countries — reveals: Companies with high-performing IT organizations are twice as likely to exceed their profitability, market share and productivity goals. IT performance improves with DevOps maturity, and strongly correlates with well-known DevOps practices. Job satisfaction is the No. 1 predictor of performance against organizational …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date