A book says "You can categorize data as variable or constant. A data item is constant when it cannot be changed after a program is compiled - in other words, when it cannot vary.
In my view after compilation you can go into the source code and change it if you want. I wish to know in the book what is meant by compilation.
Loading

VulpesRank #55 · 29.2K reputationPosted Dec 9, 2011, 1:24 PM EST
Posted Dec 9, 2011, 3:55 PM EST
Suppose we say in a program we declared const double PI = 3.14; after execution of the program we have changed the value to 10.5 therefore new value is const double PI = 10.5; if we execute the program what will happen.
VulpesRank #55 · 29.2K reputationPosted Dec 10, 2011, 2:02 AM EST
If you try to change a const value after it has been declared, the compiler will produce an error - 'the left-hand side of an assignment statement must be a variable, property or indexer.'
Posted Dec 10, 2011, 3:43 AM EST
const int HIGH = 10;
using System;
public class IfElseDecision
{
public static void Main()
{
const int HIGH = 10;
string numberString;
int number;
Console.Write("Enter an integer ");
numberString = Console.ReadLine();
number = Convert.ToInt32(numberString);
if (number > HIGH)
Console.WriteLine("{0} is greater than {1}", number, HIGH);
else
Console.WriteLine("{0} is not greater than {1}", number, HIGH);
Console.ReadKey();
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 10, 2011, 3:47 AM EST
When you make a change to the source code and then recompile, the compiler regards it as a totally new program and processes it from scratch.
However, if you try to do this, then you will get the error I described:
using System;
public class IfElseDecision
{
public static void Main()
{
const int HIGH = 10;
string numberString;
int number;
Console.Write("Enter an integer ");
numberString = Console.ReadLine();
number = Convert.ToInt32(numberString);
HIGH = 20;
if (number > HIGH)
Console.WriteLine("{0} is greater than {1}", number, HIGH);
else
Console.WriteLine("{0} is not greater than {1}", number, HIGH);
Console.ReadKey();
}
}
Posted Dec 10, 2011, 4:20 AM EST
const int HIGH = 10;
const int HIGH = 20; (in same program it is wrong)
Let us assume
int x = 2; (to initialize)
int x = 4; (in the same program this is acceptable in a appropriate place)
VulpesRank #55 · 29.2K reputationPosted Dec 10, 2011, 8:12 AM EST
Posted Dec 10, 2011, 8:29 AM EST
Sam HobbsRank #108 · 20.5K reputationPosted Dec 10, 2011, 11:17 AM EST