IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceLanguage Basics  Print This     

Constants (Cross Ref > Language Basics)

Constants

General Info: Computer Language Constants

A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

Access VBA:   Const kPI = 3.1459

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.

Syntax Example:
Const kPI = 3.1459
Const kName = "Mike"
 
//Public variable:
Public Const kFeetToMeter=3.28, kMeterToFeet=.3

 

More Info

Code:  Access VBA Constants (Const kPI = 3.1459)
Definition:  Computer Language Constants

ASP Classic:   Const kPI = 3.1459

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.

Syntax Example:
Const kPI = 3.1459
Const kName = "Mike"
 
//Public variable:
Public Const kFeetToMeter=3.28, kMeterToFeet=.3


About Computer Language Constanst
A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

More Info

Code:  ASP Classic Constants (Const kPI = 3.1459)
Definition:  Computer Language Constants

C#:   const

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.

Syntax Example:
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;
}


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: Multiple Choice

Which statement best describes C# constants?

Answer:
1. 

Declare 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.

2. 

Declare constants with the literal 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.

3. 

Declare constants with the const keyword. For global constants, you can make use of a special global _CSGlobal class.

4. 

Declare constants with the const keyword. You can make constants part of a class or global to a namespace using the global keyword.

More Info

Definition:  Computer Language Constants
 

C++:   const

In standard C++, you use const and static const to declare constants.

Syntax Example:
//C++Builder 2009 Example:
const String kName = "Mike";
const int kAge = 35;
  
ShowMessage("Hi " + kName + ", you are " + kAge + ".");

More Info

Definition:  Computer Language Constants

C++/CLI:   const or literal

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.

Syntax Example:
//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;
//...


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

1 Intermediate Level Question

Question #1: True or False?

Constants defined as static const are not viewed as a constant by the CLR runtime.

Syntax Example:

static const int PI = 3.14;
Answer:
  • True
  • False
  • More Info

    Definition:  Computer Language Constants
     

    Corel Paradox:   const..endConst

    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.

    Syntax Example:
    const
    kFeetToMeter = Number(3.2808)
    kMeterToFeet = Number(.3048)
    kName = String("Mike")
      kCA = "California"     ;String inferred.
    endConst

    Local Constants

    Local constants are declared within the method block.

    method pushButton(var eventInfo Event)
    const
    kCA = "California"
    endConst
     
      msgInfo("", "CA is short for " + kCA)
    endMethod

    Var Window Constants

    Variables declared in an object's Var window are visible to all methods attached to that object, and objects that it contains.



    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    Which of the following is the correct syntax for declaring a constant?

    Answer:
    1. 
    const
      kCA = "California"
    endConst
    2. 
    const
      kCA = "California"
    end Const
    3. 
    const
      kCA := "California"
    endConst
    4. 
    constant
      kCA = "California"
    endConstant
    5. 
    const
      kCA = 'California'
    endConst

    More Info

    Definition:  Computer Language Constants
    Code:  ObjectPAL Constants (const..endConst)

    Delphi:   Const kPI: Double=3.1459;

    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.

    Syntax Example:
    Const 
      kFeetToMeter: Double = 3.2808;
      kMeterToFeet: Double = .3048;
      kName: String = "Mike";
     
    //Local constants:
    procedure SomeProcedure;
    const
      kPI: Double=3.1459;
    begin
    end;

    Writable Typed Constants

    Delphi also supports writable typed constants which are not constants. They are initialized global variables and are a hold-over from earlier versions of Delphi and Turbo Pascal. They are turned off by default in later versions of Delphi so to use them you have to use the compiler directive {$J+} to enable then {$J-} to disable.

    Writable Typed Constants Example:

    {$J+}
    const
       clicks : Integer = 1; //not a true constant
    {$J-}
    begin
      Form2.Caption := IntToStr(clicks) ;
      clicks := clicks + 1;
    end;

    Although the Delphi help says not to use writable typed constants, you may find them useful when you wish to use static local variables. You'll have to decide if you wish to use them. The benefit #3 below has over the suggested #1 is that #3 is local and you therefore don't  have to worry about duplicate constant/variable names; otherwise, they both work well.

    //1. Global variable (suggested technique).
    //var
    //  ButtonClicks: Integer = 0;

    procedure TForm2.Button5Click(Sender: TObject);
    //2. Does not work (local variable is destroyed with each click
    //   so the compiler doesn't even allow this to compile).
    //var
    //  ButtonClicks: Integer = 0;
    //Does not compile!
    //3. Writable typed constant is global
    //   (same as more correct #3 above, but weird).

    {$J+}
    const
      ButtonClicks: Integer = 0;
    {$J-}
    begin
      If ButtonClicks >= 5 then
        ShowMessage('Stop clicking the button.')
      Else
      begin
        ButtonClicks := ButtonClicks + 1;
        Form2.Caption := IntToStr(ButtonClicks);
      end;
    end;


    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    Which example uses the correct syntax to declare a constant?

    Answer:
    1. 

    const
      kPI : Double := 3.1459;

    2. 


    const
      kPI Double := 3.1459;


    3. 


    const
      kPI Double = 3.1459;


    4. 

    const
      kPI : Double = 3.1459;

    5. 

    const
      kPI As Double := 3.1459;


    More Info

    Definition:  Computer Language Constants
    Code:  Delphi Constants (Const kPI: Double=3.1459;)

    Delphi Prism:   const kPI: Double=3.1459;

    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.

    Syntax Example:
    //Specified type:
    const
    kFeetToMeter: Double = 3.2808;
      kMeterToFeet: Double = .3048; 
      kName: String = "Mike";

    //Unspecified type:
    const kPIShort = 3.14;

    About Computer Language Constants
    A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    Which code fragment uses the correct syntax to declare a constant?

    Answer:
    1. 

    const

      kPI Double = 3.1459;

    2. 

    const

      kPI : Double := 3.1459;

    3. 

    const

      kPI As Double = 3.1459;

    4. 

    const

      kPI As Double := 3.1459;

    5. 

    const

      kPI : Double = 3.1459;

    More Info

    Definition:  Computer Language Constants
    Code:  Delphi Prism Constants (const kPI: Double=3.1459;)

    JavaScript:   Not Supported

    Although JavaScript has variables, it does not have constants.

    Perl:   use constant

    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.

    Syntax Example:
    use constant FULL_NAME => 'Mike Prestwood';
    use constant AGE => 38;
      
    print "Your name is " . FULL_NAME . ".<br>";
    print "You are " . AGE . ".<br>";

    More Info

    Definition:  Computer Language Constants

    PHP:   define

    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.

    Syntax Example:
    define('FULL_NAME', 'Mike Prestwood');
    define("AGE", 25);
      
    echo "Your name is " . FULL_NAME . ".";
    echo "You are " . AGE . ".";

    Built-In Constants

    The PHP website has a list of built-in predefined constants.

    More Info

    Definition:  Computer Language Constants

    VB Classic:   Const kPI = 3.1459

    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.

    Syntax Example:
    Const kPI = 3.1459
    Const kName = "Mike"
     
    //Public variable:
    Public Const kFeetToMeter=3.28, kMeterToFeet=.3

     

    More Info

    Definition:  Computer Language Constants
    Code:  VB Classic Constants (Const kPI = 3.1459)

    VB.Net:   Const kPI Double = 3.1459

    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.

    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.

    Syntax Example:
    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.3048
    End Class


    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    Which statement best describes VB.Net constants?

    Answer:
    1. 

    In VB.Net, you define constants with the Constant keyword. All constants are part of a class but you can have special global constants by making use of the special _Global class.

    2. 

    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.

    3. 

    In VB.Net, you define constants with the Literal keyword. All constants are part of a class (no global constants) and you must create a class prior to accessing the constant using ClassName.ConstantName.

    4. 

    In VB.Net, you define constants with the Const keyword. You can declare constants witin a class or globally outside a class. For class constants, you can have access to it using ClassName.ConstantName so long as you have added the class to the project.

    More Info

    Definition:  Computer Language Constants
    Code:  VB.Net Constants (Const kPI Double = 3.1459)




    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


    ©1995-2024 Prestwood IT Solutions.   [Security & Privacy]