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: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

  • The exponential growth of data, along with virtualization, is bringing a disruptive level of complexity to your IT infrastructure. Having multiple point solutions for data protection is not the answer, as it adds to the chaos and impedes on your ability to deliver consistent SLAs. Read this white paper to learn how a more holistic view of the infrastructure can help you to unify the data protection schemas by properly evaluating your business needs in order to gain a thorough understanding of the applications …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds