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
Sam HobbsPosted Dec 10, 2011, 4:17 PM
Posted Dec 10, 2011, 1:29 PM
VulpesPosted Dec 10, 2011, 1:12 PM
Posted Dec 10, 2011, 9:20 AM
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)
VulpesPosted Dec 10, 2011, 8:47 AM
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, 8:43 AM
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();
}
}
VulpesPosted Dec 10, 2011, 7:02 AM
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 9, 2011, 8:55 PM
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.
VulpesPosted Dec 9, 2011, 6:24 PM