Creating Flexible Constant Fields

Discover how to use constant fields without having to hard code the values into the class.

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;
}



Author: Jay Miller

Read full article on Developer.com
Regional Articles
- Creating Flexible Constant Fields Alabama
- Creating Flexible Constant Fields Alaska
- Creating Flexible Constant Fields Arizona
- Creating Flexible Constant Fields Arkansas
- Creating Flexible Constant Fields California
- Creating Flexible Constant Fields Colorado
- Creating Flexible Constant Fields Connecticut
- Creating Flexible Constant Fields DC
- Creating Flexible Constant Fields Delaware
- Creating Flexible Constant Fields Florida
- Creating Flexible Constant Fields Georgia
- Creating Flexible Constant Fields Hawaii
- Creating Flexible Constant Fields Idaho
- Creating Flexible Constant Fields Illinois
- Creating Flexible Constant Fields Indiana
- Creating Flexible Constant Fields Iowa
- Creating Flexible Constant Fields Kansas
- Creating Flexible Constant Fields Kentucky
- Creating Flexible Constant Fields Louisiana
- Creating Flexible Constant Fields Maine
- Creating Flexible Constant Fields Maryland
- Creating Flexible Constant Fields Massachusetts
- Creating Flexible Constant Fields Michigan
- Creating Flexible Constant Fields Minnesota
- Creating Flexible Constant Fields Mississippi
- Creating Flexible Constant Fields Missouri
- Creating Flexible Constant Fields Montana
- Creating Flexible Constant Fields Nebraska
- Creating Flexible Constant Fields Nevada
- Creating Flexible Constant Fields New Hampshire
- Creating Flexible Constant Fields New Jersey
- Creating Flexible Constant Fields New Mexico
- Creating Flexible Constant Fields New York
- Creating Flexible Constant Fields North Carolina
- Creating Flexible Constant Fields North Dakota
- Creating Flexible Constant Fields Ohio
- Creating Flexible Constant Fields Oklahoma
- Creating Flexible Constant Fields Oregon
- Creating Flexible Constant Fields Pennsylvania
- Creating Flexible Constant Fields Rhode Island
- Creating Flexible Constant Fields South Carolina
- Creating Flexible Constant Fields South Dakota
- Creating Flexible Constant Fields Tennessee
- Creating Flexible Constant Fields Texas
- Creating Flexible Constant Fields Utah
- Creating Flexible Constant Fields Vermont
- Creating Flexible Constant Fields Virginia
- Creating Flexible Constant Fields Washington
- Creating Flexible Constant Fields West Virginia
- Creating Flexible Constant Fields Wisconsin
- Creating Flexible Constant Fields Wyoming

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML