Best Practices for Fixed Data Processing                                        
•        Always prefer constant data or read-only field data to ‘magic numbers’.
•        Recall that constant data can only be used if the value is known at compile time, as the value is hard
coded in CIL.  Constant data must be assigned at the point of declaration.
•        Read-only values can be assigned within a constructor (even if the value is not known at compile time)
or at the point of declaration but nowhere else.
// C#
class MyConstants
{
// const data must be known at runtime.
// const data is implicitly static.
public const float PI = 3.14F;

// Readonly fields can be assigned in ctors
// and are NOT implicitly static.
public readonly static float currentInterestRate;

static MyConstants()
{ currentInterestRate = GetInterestRate(); }

private static float GetInterestRate()
{
   // Assume we are reading from a database here...
   return 6.0F;
}
}


' VB 2005
Class MyConstants
' const data must be known at runtime.
' const data is implicitly static.
Public Const PI As Single = 3.14F

' Readonly fields can be assigned in ctors
' and are NOT implicitly static.
Public Shared ReadOnly currentInterestRate As Single

Shared Sub New()
   currentInterestRate = GetInterestRate()
End Sub
Private Shared Function GetInterestRate() As Single
   ' Assume we are reading from a database here...
   Return 6.0F
End Function
End Class
Fixed Data Processing
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials