A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.
In Delphi, you define constants similar to how you define variables but use the Const keyword instead of the Var keyword. Declare global constants in a unit's interface section and unit constants (scope limited to unit) in the implementation section. Declare local constants above the begin..end block.
Const
kFeetToMeter: Double = 3.2808;
kMeterToFeet: Double = .3048;
kName: String = "Mike";
//Local constants:
procedure SomeProcedure;
const
kPI: Double=3.1459;
begin
end;
Scope can be Public, Global, or Private. The use of the newer Public keyword is preferred to the older Global. Private Const is the same as just specifying Const.
Const kPI = 3.1459
Const kName = "Mike"
//Public variable:
Public Const kFeetToMeter=3.28, kMeterToFeet=.3
Scope can be Public or Private. Public Const is the same as just specifying Const. As with variables, all constants are variants. You do not specify the type, it's implied.
In C#, you define constants with the const keyword.
All constants are part of a class (no global constants) but you can make a constant public and have access to it using ClassName.ConstantName so long as you have added the class to the project. This works even without creating the class as if the public constants were static, but you cannot use the static keyword.
Constants must be of an integral type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string), an enumeration, or a reference to null.
public class Convert : Object{ public const string kName = "Mike";
//You can declare multiple of the same type too:
const Double kFeetToMeter = 3.2808, kMeterToFeet = .3048;}
In standard C++, you use const and static const to declare constants.
//C++Builder 2009 Example:
const String kName = "Mike";const int kAge = 35;
ShowMessage("Hi " + kName + ", you are " + kAge + ".");
C++/CLI supports the const and static const keywords of standard C++ as well as the new literal keyword. A literal is equivalent to static const in standard C++ and Microsoft's documentation recommends to replace static const with the new literal keyword because a literal is available in metadata; a static const variable is not available in metadata to other compilers.
You can use static const within the class declaration or locally within a method. However, literal is only valid in the class declaration section and const is only valid within a method.
//some method {
const String^ MyName = "John";static const Int32 MyAge = 27;
//}
// public class SomeClass : public Object {public: literal double Pi = 3.14159; literal String^ MyName = "Mike"; static const Int32 MyAge = 35;
//...
In ObjectPAL, you declare one or more constant values within a const..endConst block. Optionally, you can specify the dataType by casting the value as part of the declaration.
If you do not specify the data type, the data type is inferred from the value as either a LongInt, a Number, a SmallInt, or a String.
As with variables, the const..endConst block can come within a method or procedure as the first bit of code, or in the Const window. Putting it above the method or procedure is allowed but has no significance so don't.
const kFeetToMeter = Number(3.2808) kMeterToFeet = Number(.3048) kName = String("Mike")
kCA = "California" ;String inferred.endConst
In Prism, you define constants similar to how you define variables but use the Const keyword instead of the Var keyword. Specifying the type is optional. If you don't specify the type, the compiler chooses the most appropriate type for you.
Declare class constants as part of the class definitions. Declare local constants above the begin..end. Although Prism support inline variables, inline constants are not supported.
//Specified type:const kFeetToMeter: Double = 3.2808;
kName: String = "Mike"; //Unspecified type:
const kPIShort = 3.14;
Although JavaScript has variables, it does not have constants.
In Perl, you declare constants using the use constant keywords:
use constant CONST_NAME => "Value";
Constants in Perl are case sensitive. A common standard in Perl is to use all-uppercase letters, with underscores to separate words within the name.
use constant FULL_NAME => 'Mike Prestwood';use constant AGE => 38;
print "Your name is " . FULL_NAME . ".<br>";print "You are " . AGE . ".<br>";
In PHP, you declare constants using the define keyword:
define("CONST_NAME", "Value");
Constants in PHP are case sensitive. A common standard in PHP is to use all-uppercase letters, with underscores to separate words within the name.
define('FULL_NAME', 'Mike Prestwood');define("AGE", 25);
echo "Your name is " . FULL_NAME . ".";echo "You are " . AGE . ".";
In VB.Net, you define constants with the Const keyword.
All constants are part of a class (no global constants) but you can make a constant public and have access to it using ClassName.ConstantName so long as you have added the class to the project. This works even without creating the class as if the public constants were static, but you cannot use the Shared keyword.
Public Class Convert Inherits System.Object
Public Const kName As String = "Mike"
Private Const kPI Double = 3.1459
//Declare two or more on same line too: Const kFeetToMeter = 3.2808, kMeterToFeet = 0.3048End Class