Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Named Constants for Your Components

Visual Studio .NET 2003

Constants provide a convenient way to set and refer to values that are not expected to change. Individual constants may be declared and referred to in code, or sets of related constants may be declared as enumerations.

A constant is declared by using the Const keyword (const in C#). A constant may represent any intrinsic or enumeration data type (for example, Integer, String, Decimal), but may not be a structure or an instance of a class. Constants use the following syntax:

' Visual Basic
Public Const SpeedOfLightInKMPerSecond as Integer = 300000
Private Const ColorOfTheSky as String = "Blue"
Const DensityOfWater as Integer = 1

//C#
public const int SpeedOfLightInKMPerSecond = 300000;
private const string ColorOfTheSky = "Blue";
const int DensityOfWater = 1;

Constants and enumerations follow the same access rules as variables. A class-level constant or enumeration may be modified using the same access modifiers (for example, Public) as a variable. A procedure-level constant is visible only within the context of the procedure. Enumerations cannot be declared at the procedure level, and must be declared at the class or namespace level. Public constants are not recommended due to the possibility of name confusion. If you wish to have global constants, it is best to use an enum.

Enumerations

Enumerations allow you to define a set of related constants. For instance, you might create an enumeration for the days of the week, as in the following example:

' Visual Basic
Public Enum DaysOfTheWeek
   mydaySaturday = 1
   mydaySunday = 2
   mydayMonday = 3
   mydayTuesday = 4
   mydayWednesday = 5
   mydayThursday = 6
   mydayFriday = 7
End Enum

 //C#
public enum DaysOfTheWeek
{
   mydaySaturday = 1,
   mydaySunday = 2,
   mydayMonday = 3,
   mydayTuesday = 4,
   mydayWednesday = 5,
   mydayThursday = 6,
   mydayFriday = 7
}

The members of an enumeration need not be sequential. Thus, if you have some general-purpose numeric constants you wish to define for your component, you can put them into a general enum.

' Visual Basic
Private Enum General
   genFeetInAMile = 5280
   genIgnitionTemp = 451
End Enum

// C#
private enum General
{
   genFeetInAMile = 5280,
   genIgnitionTemp = 451,
}

Data Types for Enumerations

The default data type for enumerations in Visual Basic is Integer, but they may be either Byte, Short, Integer, or Long. In C#, it can be any of the integral types except char. The default is int. If you wish to provide constants of a data type other than those supported in enumerations (such as strings, decimals, and so on), you should do so by using the Const (const in C#) keyword.

To declare an enumeration as a variable type other than integer, use the following syntax:

' Visual Basic
Enum AccountType as Byte
   myAccountSavings = 1
   myAccountChecking = 2
End Enum

// C#
enum AccountType : byte
{
   myAccountSavings = 1,
   myAccountChecking = 2
}

See Also

Constants and Enumerations | Enum Statement | const | enum

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.