Constants and Enums window

The Constants and Enums window lets you do a constant analysis. You can access this window via the View menu.

Constants and Enums window

All constants and Enums
List all constants and enumeration constants in the system.
Duplicate names
List all the cases where 2 or more constants/enum constants share the same name.
Duplicate values
List all the cases where 2 or more constants/enum constants share the same value. Use this option to search for constants to join together. Defining a constant value at just one location is good for both reuse and prevention of errors. When you need to change the value, you only need to do it once.
Duplicate name+value
List all the cases where 2 or more constants/enum constants share the same name and value. You should remove the duplicates if possible.
Same name, different value
List all the cases where 2 or more constants/enum constants share the same name but have a different value. These are potential errors. Why do they have a different value? Is it by design or by error? Even if the code is correct now, having several different values is likely to confuse developers later. Consider renaming the constants. Right-click the one you want to rename to get a list of references. After renaming, remember to rewrite each of the use locations as well, otherwise your code will not function the way it did before.
Same value, different name
List all the cases where 2 or more different constants/enum constants share the same value. This list may reveal constants that have been defined several times with (slightly) differing names. As described above, defining each constant at just one location is better.

The toolbar buttons let you access more features. You can also access these features by right-clicking the list.

Guidelines for constants

  1. Define each constant just once. Reuse your constants across the entire system. When you need to change the value, you only need to change it once.
  2. One name must mean one thing. The same name shouldn't refer to many values depending on code location. — The one exception is Enum constants in VB.NET. Each Enum constant is always prefixed with the Enum name (MyEnum.MyConst), making confusion less likely.
  3. Use descriptive names for both the constants and the enumerations.
  4. Centralize constant definitions in a special global module when it seems sensible. When the constant is only used in a certain module or procedure, you can also keep it local.
  5. Centralize your strings. In classic VB, each string gets written into the executable every time it occurs in the code. By defining your strings as constants you can reuse the strings without bloating the executable. VB.NET does a better job at this as it saves each string just once. See String literal analysis.
  6. Don't use literal numeric values in your code. Declare constants and Enums instead. Constants make your code more self-documenting. Again, when you need to change the value, you only need to do it once.
  7. Prefer Enums over constants. An Enum is a better choice for related constants. You can use the Enum as a data type for variables and parameters, which makes them more self-documenting.
  8. Add enough precision to floating-point values. Reuse is more successful when constants are accurate. As an example, declare PI=3.14159265358979 rather than PI=3.14. Even if the program doesn't need all the decimals right now, reusing an inaccurate value may lead to problems later. An inaccurate or incorrect constant value may be hard to detect.

© Project Analyzer Help