Home

Using Variables: Constants

 

Custom Constants

Suppose you intend to use a number such as 39.37 over and over again. Here is an example:

using System;

public class Exercise
{
    static void Main()
    {
	double Meter, Inch;
		
	Meter = 12.52;
	Inch = Meter * 39.37;
        Console.Write(Meter);
        Console.Write("m = ");
        Console.Write(Inch);
        Console.WriteLine("in\n");
    }
}

Here is an example of running the program:

12.52m = 492.9124in

If you use this 39.37 many times in your program, at one time, you may make a mistake and type it as 3937 or 3.937 or else. Consider the following program:

using System;

class Program
{
    static void Main()
    {
        double Meter, Inch;

        Meter = 12.52;
        Inch = Meter * 39.37;
        Console.Write(Meter);
        Console.Write("m = ");
        Console.Write(Inch);
        Console.WriteLine("in\n");

        Meter = 12.52;
        Inch = Meter * 3.937;
        Console.Write(Meter);
        Console.Write("m = ");
        Console.Write(Inch);
        Console.WriteLine("in\n");

        Meter = 12.52;
        Inch = Meter * 393.7;
        Console.Write(Meter);
        Console.Write("m = ");
        Console.Write(Inch);
        Console.WriteLine("in\n");
    }
}

This would produce:

12.52m = 492.9124in

12.52m = 49.29124in

12.52m = 4929.124in

Because of mistakes in the way to represent the number, the same calculation produces different results. To make sure that this is unlikely, you can instead use a variable that holds the value. Then, when you need that value, you can access the variable instead of the value itself. A number such as 39.37 is called a constant.

A constant is a value that never changes such as 244, "ASEC Mimosa", 39.37, or True. These are constant values you can use in your program any time. You can also declare a variable and make it a constant; that is, use it so that its value is always the same.

To create a constant, type the const keyword to its left. When declaring a constant, you must initialize it with an appropriate value. Here is an example:

const double ConversionFactor = 39.37;

Once a constant has been created and it has been appropriately initialized, you can use its name where the desired constant would be used. Here is an example of a constant variable used various times:

using System;

class Exercise
{
    static void Main()
    {
        const double ConversionFactor = 39.37;
        double Meter, Inch;

        Meter = 12.52;
        Inch = Meter * ConversionFactor;
        Console.Write(Meter);
        Console.Write("m = ");
        Console.Write(Inch);
        Console.WriteLine("in\n");

        Meter = 12.52;
        Inch = Meter * ConversionFactor;
        Console.Write(Meter);
        Console.Write("m = ");
        Console.Write(Inch);
        Console.WriteLine("in\n");

        Meter = 12.52;
        Inch = Meter * ConversionFactor;
        Console.Write(Meter);
        Console.Write("m = ");
        Console.Write(Inch);
        Console.WriteLine("in\n");
    }
}

This would produce:

12.52m = 492.9124in

12.52m = 492.9124in

12.52m = 492.9124in

Notice that, this time, the calculation is more accurate. Also, this time, if you mistype the name of the variable in an operation, you would receive a compiler error, giving you the time to fix it.

To initialize a constant variable, the value on the right side of the assignment operator "=" must be a constant or a value that the compiler can determine as constant. Instead of using a known constant, you can also assign it another variable that has already been declared as constant.

Built-in Constants

There are two main categories of constants you will use in your programs. You can create your own constant as we saw above. The C# language also provides various constants. Some constants are part of the C# language. Some other constants are part of the .NET Framework. Before using a constant, of course, you must first know that it exists. Second, you must know how to access it. A constant that is part of the C# language can be accessed anywhere in your code. Those constant are normally defined in the System namespace. Other constant arte defined in various appropriate namespaces.

null: The null keyword is a constant used to indicate that a variable doesn't hold a known value

PI: PI is a constant used as the ratio of the circumference of a circle to its diameter. PI is defined in Math. To use it, you would type Math.PI.

 

Previous Copyright © 2006-2007 FunctionX, Inc. Home