A Double-Word
|
Introduction
|
A double-word is a group of two consecutive Words. This
means that a double-word combines 4 bytes or 32 bits. The bits, counted from
right to left, start at 0 and end at 31. The most right bit, bit 0, is
called the low order bit or LO bit or LOBIT. The most left bit, bit
31, is called the high order bit or HI bit or HIBIT. The other bits
are called using their positions.
|
2n-1 | 230 | 229 | 228 | 227 | 226 | 225 | 224 |
etc | 1,073,741,824 | 536,870,912 | 268,435,456 | 134,217,728 | 67,108,864 | 33,554,432 | 16,777,216 |
223 | 222 | 221 | 220 | 219 | 218 | 217 | 216 |
8,388,608 | 4,194,304 | 2,097,152 | 1,048,576 | 524,288 | 262,144 | 131,072 | 65,536 |
215 | 214 | 213 | 212 | 211 | 210 | 29 | 28 |
32,768 | 16,384 | 8,192 | 4,096 | 2,048 | 1,024 | 512 | 256 |
27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1*231+1*230+1*229 +
1*228 + 1*227 + 1*226 + 1*225
+ 1*224 + 1*223 + 1*222 + 1*221
+ 1*220 + 1*219 + 1*218 + 1*217
+ 1*216 + 1*215 + 1*214 + 1*213
+ 1*212 + 1*211 + 1*210 + 1*29
+ 1*28 + 1*27 + 1*26 + 1*25
+ 1*24 + 1*23 + 1*22 + 1*21
+ 1*20
= 2,147,483,648 + 1,073,741,824 + 536,870,912 +
268,435,456 + 134,217,728 + 67,108,864 + 33,554,432 + 16,777,216 +
8,388,608 + 4,194,304 + 2,097,152 + 1,048,576 + 524,288 + 262,144 +
131,072 + 65,536 + 32,768 + 16,384 + 8,192 + 4,096 + 2,048 + 1,024 + 512
+ 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
= 4,286,578,708
The minimum hexadecimal value you can store in a
double-word is 0x00000000000000000000000000000000 which is the same as
0x0. To find out the maximum hexadecimal number you can represent with a
word, replace every group of 4-bits with an f or F:
1111 | 1111 | 1111 | 1111 | 1111 | 1111 | 1111 | 1111 |
f | f | f | f | f | f | f | f |
= 0xffffffff = 0xFFFFFFFF |
To declare a variable that can hold large values,
you can use the var keyword and initialize the variable with the
desired value. Here is an example:
class Exercise
{
static void Main()
{
var population = 72394475;
System.Console.Write("Country Population: ");
System.Console.WriteLine(population);
}
}
This would produce:
Country Population: 72394475 Press any key to continue . . .
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New Project...
- In the middle list, click Empty Project
- Change the Name to gdcs3 and click OK
- To create a file for the code, on the main menu, click Project -> Add New Item...
- In the middle list, click Code File
- Change the Name to CleaningOrder
- Click Add
- In the empty document, type the following:
class Order { static void Main() { byte? shirts = null; byte? pants = null; ushort? otherItems = null; shirts = 4; pants = 0; otherItems = 3; System.Console.WriteLine("-/- Georgetown Cleaning Services -/-"); System.Console.WriteLine("========================"); System.Console.WriteLine("------------------------"); System.Console.WriteLine("Item Type Qty"); System.Console.WriteLine("------------------------"); System.Console.Write("Shirts "); System.Console.WriteLine(shirts); System.Console.Write("Pants "); System.Console.WriteLine(pants); System.Console.Write("Other Items "); System.Console.WriteLine(otherItems); System.Console.WriteLine("========================"); System.Console.ReadKey(); } }
- Execute the program to see the result
- Close the DOS window
A double-word is large enough to contain double the
amount of data that can be stored in a word. This is equivalent to 32
bits or 4 bytes or 4,294,967,295. Therefore, a double-word is used for
large numbers that would not fit in a word.
To use a variable that would hold quite large
numbers, besides the var keyword, you can declare it using the
int keyword. A variable declared as int can store values
between 2,147,483,648 and 2,147,484,647 negative or positive, that can
fit in 32 bits.
Here are examples:
class Exercise
{
static void Main()
{
int coordX;
int coordY;
coordX = 12;
coordY = -8;
System.Console.Write("Cartesian Coordinate System: ");
System.Console.Write("P(");
System.Console.Write(coordX);
System.Console.Write(", ");
System.Console.Write(coordY);
System.Console.WriteLine(")\n");
}
}
When executed, the program would produce:
Cartesian Coordinate System: P(12, -8)
If you declare an integer variable using the var
keyword and initialize it with a value lower than 2,147,484,647, the
compiler concludes that the memory needed to store that variable is 32
bits:
When initializing an integral variable, instead of a
decimal number, you can also initialize it with a hexadecimal value
whose decimal equivalent is less than 2,147,484,647. Here is an example:
class Exercise
{
static void Main()
{
var number = 0xF0488EA;
System.Console.Write("Number: ");
System.Console.WriteLine(number);
}
}
This would produce:
Number: 251955434 Press any key to continue . . .
If the variable must hold only positive natural
numbers, you can declared it using the uint keyword, which
represents an unsigned integer. The uint keyword is used to
identify a 32-bit positive integer whose value would range from 0 to
4,294,967,295. Here are examples:
class Exercise
{
static void Main()
{
uint dayOfBirth;
uint monthOfBirth;
uint yearOfBirth;
dayOfBirth = 8;
monthOfBirth = 11;
yearOfBirth = 1996;
System.Console.WriteLine("Red Oak High School");
System.Console.Write("Student Date of Birth: ");
System.Console.Write(monthOfBirth);
System.Console.Write("/");
System.Console.Write(dayOfBirth);
System.Console.Write("/");
System.Console.Write(yearOfBirth);
}
}
This would produce:
Red Oak High School Student Date of Birth: 11/8/1996
|
- To use unsigned variables, change the file as follows:
class Order { static void Main() { byte? shirts = null; byte? pants = null; ushort? otherItems = null; uint? orderDay = null; uint? orderMonth = null; uint? orderYear = null; shirts = 4; pants = 0; otherItems = 3; orderDay = 15; orderMonth = 7; orderYear = 2002; System.Console.WriteLine("-/- Georgetown Cleaning Services -/-"); System.Console.WriteLine("========================"); System.Console.Write("Order Date: "); System.Console.Write(orderMonth); System.Console.Write('/'); System.Console.Write(orderDay); System.Console.Write('/'); System.Console.WriteLine(orderYear); System.Console.WriteLine("------------------------"); System.Console.WriteLine("Item Type Qty"); System.Console.WriteLine("------------------------"); System.Console.Write("Shirts "); System.Console.WriteLine(shirts); System.Console.Write("Pants "); System.Console.WriteLine(pants); System.Console.Write("Other Items "); System.Console.WriteLine(otherItems); System.Console.WriteLine("========================"); System.Console.ReadKey(); } }
- Execute the program. This would produce:
-/- Georgetown Cleaning Services -/- ======================== Order Date: 7/15/2002 ------------------------ Item Type Qty ------------------------ Shirts 4 Pants 0 Other Items 3 ========================
- Close the DOS window
Introduction
|
Sometimes you may want to store values that a
double-word cannot handle. To store a very large number in a variable,
you can consider a combination of 64 bits. The group can also be
referred to as a quad-word. A quad-word is so large it can store numbers
in the range of -9,223,372,036,854,775,808 and
9,223,372,036,854,775,807.
If you declare an integer variable using the var
keyword and initialize it with a value between 2,147,484,647 and
9,223,372,036,854,775,807, the compiler concludes that the memory needed
to store that variable is 64 bits:
If you want to use a variable that can hold very
large numbers that would require up to 64 bits, you can declare it using
either the var or the long keyword.
As stated previously, if you initialize the variable
with a value lower than 2,147,484,647, the compiler would allocate 32
bits of memory for it. If you initialize the variable with a value
between 2,147,484,647 and 9,223,372,036,854,775,807, the compiler would
allocate 64 bits of memory for it. If the value is higher than
9,223,372,036,854,775,807, which is too large, the compiler would
present an error:
This means that you should limit the values assigned
to integral variables to 64 bits, which is very significant.
Here is an example:
class Exercise
{
static void Main()
{
var countryArea = 5638648;
System.Console.Write("Country Area: ");
System.Console.Write(countryArea);
System.Console.Write("km2\n");
}
}
This would produce:
Country Area: 5638648km2 Press any key to continue . . .
As mentioned for other integral types, you can
initialize a long variable with a hexadecimal value.
Although the long data type is used for large
numbers, it mainly indicates the amount of space available but you don't
have to use the whole space. For example, you can use the long
keyword to declare a variable that would hold the same range of numbers
as the short, the int, or the uint data types. If
you declare a variable as long but use it for small
numbers that don't require 64 bits, the compiler would allocate the
appropriate amount of space to accommodate the values of the variable.
Consequently, the amount of space made available may not be as large as
64 bits. If you insist and want the compiler to reserve 64 bits, when
assigning a value to the variable, add an L suffix to it. Here is
an example that uses a long variable to store a number
that would fit in 32 bits:
class Exercise
{
static void Main()
{
long countryArea;
countryArea = 5638648L;
System.Console.Write("Country Area: ");
System.Console.Write(countryArea);
System.Console.Write("km2\n");
}
}
Therefore, keep in mind that an int, a
uint, a short, or a ushort can fit in a long variable.
You can use a combination of 64 bits to store
positive or negative integers. In some cases, you will need a variable
to hold only positive, though large, numbers. To declare such a
variable, you can use the ulong data type. A variable declared as
ulong can handle extremely positive numbers that range from 0 to
18,446,744,073,709,551,615 to fit in 64 bits.
A real number is a number that displays a decimal
part. This means that the number can be made of two sections separated
by a symbol that is referred to as the Decimal Separator or Decimal
Symbol. This symbol is different by language, country, group of
languages, or group of countries. In US English, this symbol is the
period as can be verified from the Regional (and Language) Settings of
the Control Panel:
On both sides of the decimal symbol, digits are used
to specify the value of the number. The number of digits on the right
side of the symbol determines how much precision the number offers.
The integers we have used so far have the main
limitation of not allowing decimal values. C# provides floating values
that would solve this problem. The most fundamental floating variable is
declared with the float keyword. A variable declared as
float can store real numbers that range from ±1.5 x 10−45
to ±3.4 x 1038 with a precision of 7 digits in 32 bits.
Here is an example:
class Exercise
{
static void Main()
{
float distance;
}
}
When a variable is larger than the float can
handle and requires more precision, you should declare it using either
the var or the the double keyword. Here is an example:
class Exercise
{
static void Main()
{
var number = 62834.9023;
System.Console.Write("Number: ");
System.Console.WriteLine(number);
}
}
This would produce:
Number: 62834.9023 Press any key to continue . . .
A variable declared as double uses 64 bits to
store very large numbers ranging from ±5.0 x 10−324 to
±1.7 x ÂÂ10308 with a precision of 15 or 16 digits. Because
the double data type provides a better result with better
precision than the float, whenever you declare a variable using
either the var or the float keyword and assign it a value,
the compiler allocates 64 bits to store the values of the variable. If
you insist on the variable being treated as float, when assigning
it a value, add an f or an F suffix to the value. Here is
an example:
class Exercise
{
static void Main()
{
float distance;
distance = 248.38F;
System.Console.Write("Distance = ");
System.Console.Write(distance);
System.Console.WriteLine("km\n");
}
}
This would produce:
Distance = 248.38km
Remember that if you declare the variable as var
and want to treat it as a value with single precision, add an f
or an F suffix to the value assigned to it. Here is an
example:
class Exercise
{
static void Main()
{
var number = 62834.9023F;
System.Console.Write("Number: ");
System.Console.WriteLine(number);
}
}
On the other hand, if you want a value to be treated
with double-precision, add a d or a D suffix to
it. Here is an example:
class Exercise
{
static void Main()
{
var number = 62834.9023D;
System.Console.Write("Number: ");
System.Console.WriteLine(number);
}
}
|
- To use a double-precision value, change the file as follows:
class Order { static void Main() { byte? shirts = null; byte? pants = null; ushort? otherItems = null; uint? orderDay = null; uint? orderMonth = null; uint? orderYear = null; double? mondayDiscount = null; shirts = 4; pants = 0; otherItems = 3; orderDay = 15; orderMonth = 7; orderYear = 2002; mondayDiscount = 0.25D; // 25% System.Console.WriteLine("-/- Georgetown Cleaning Services -/-"); System.Console.WriteLine("========================"); System.Console.Write("Order Date: "); System.Console.Write(orderMonth); System.Console.Write('/'); System.Console.Write(orderDay); System.Console.Write('/'); System.Console.WriteLine(orderYear); System.Console.WriteLine("------------------------"); System.Console.WriteLine("Item Type Qty"); System.Console.WriteLine("------------------------"); System.Console.Write("Shirts "); System.Console.WriteLine(shirts); System.Console.Write("Pants "); System.Console.WriteLine(pants); System.Console.Write("Other Items "); System.Console.WriteLine(otherItems); System.Console.WriteLine("------------------------"); System.Console.Write("Monday Discount: "); System.Console.Write(mondayDiscount); System.Console.WriteLine('%'); System.Console.WriteLine("========================"); System.Console.ReadKey(); } }
- Execute the application to see the result:
-/- Georgetown Cleaning Services -/- ======================== Order Date: 7/15/2002 ------------------------ Item Type Qty ------------------------ Shirts 4 Pants 0 Other Items 3 ------------------------ Monday Discount: 0.25% ========================
- Close the DOS window
The decimal data type can be used to declare
a variable that would hold significantly large values that can be stored
in a combination of 128 bits. You declare such a variable using the
decimal keyword. The values stored in a decimal variable can range
from ±1.0 x 10−28 to ±7.9 x 1028 with a
precision of 28 to 29 digits. Because of this high level of precision,
the decimal data type is suitable for currency values.
After declaring a decimal variable, you can
initialize it with a natural number. To indicate that the variable holds
a decimal value, when initializing it, add an m or an M
suffix to its value. Here is an example:
class Exercise { static void Main() { decimal hourlySalary; hourlySalary = 24.25M; System.Console.Write("Hourly Salary = "); System.Console.WriteLine(hourlySalary); System.Console.WriteLine(); } }
This would produce:
Hourly Salary = 24
As seen in previous sections and this one, when
declaring and initializing a real variable, the suffix you give to its
assigned value indicates to the compiler the actual type of value and
the type of memory that would be allocated for the variable:
- If the value receives an f or an F
suffix, it is considered a floating point number
with single precision
- If the value receives a d or a D suffix,
it is considered a floating point number with double
precision
- If the value receives an m or an M
suffix, it is considered a large decimal number
Consider the following program:
class Program { static void Main() { System.Console.Write("560 / 672 = "); System.Console.WriteLine(560 / 672); } }
The purpose of this program is to get the division
of 560 by 672. This would produce:
560 / 672 = 0 Press any key to continue . . .
Notice that, when such numbers are submitted to the
program, the compiler decides that these are integers and the result
produces 0. If you write such an equation and want the result to appear
as a real number, you must explicitly indicate it to the compiler. One
way you can do this consists of adding decimal places to at least one of
the numbers. Here is an example:
class Program { static void Main() { System.Console.Write("560 / 672 = "); System.Console.WriteLine(560.00 / 672.00); } }
This time, the compiler will conclude that the value
is a floating-point number; but which one? By default, the compiler
would apply the double data type. This version of the
program would produce:
560 / 672 = 0.833333333333333 Press any key to continue . . .
If you want to indicate that the value is a
float, a double, or a decimal,
add the appropriate prefix to it: f, F, d, D,
m, or M. Here are examples:
class Program { static void Main() { System.Console.Write("560 / 672 = "); System.Console.WriteLine(560F / 672f); } }
This version of the program would produce:
560 / 672 = 0.8333333 Press any key to continue . . .
|
- To use decimal variables, change the file as follows:
class Order { static void Main() { byte? shirts = null; decimal? priceOneShirt = null; byte? pants = null; decimal? priceAPairOfPants = null; ushort? otherItems = null; decimal? priceOtherItems = null; uint? orderDay = null; uint? orderMonth = null; uint? orderYear = null; double? mondayDiscount = null; shirts = 5; priceOneShirt = 0.95M; pants = 2; priceAPairOfPants = 1.95M; otherItems = 3; priceOtherItems = 4.55M; orderDay = 15; orderMonth = 7; orderYear = 2002; mondayDiscount = 0.25D; // 25% System.Console.WriteLine("-/- Georgetown Cleaning Services -/-"); System.Console.WriteLine("========================"); System.Console.Write("Order Date: "); System.Console.Write(orderMonth); System.Console.Write('/'); System.Console.Write(orderDay); System.Console.Write('/'); System.Console.WriteLine(orderYear); System.Console.WriteLine("------------------------"); System.Console.WriteLine("Item Type Qty Unit Price"); System.Console.WriteLine("------------------------"); System.Console.Write("Shirts "); System.Console.Write(shirts); System.Console.Write(" "); System.Console.WriteLine(priceOneShirt); System.Console.Write("Pants "); System.Console.Write(pants); System.Console.Write(" "); System.Console.WriteLine(priceAPairOfPants); System.Console.Write("Other Items "); System.Console.Write(otherItems); System.Console.Write(" "); System.Console.WriteLine(priceOtherItems); System.Console.WriteLine("------------------------"); System.Console.Write("Monday Discount: "); System.Console.Write(mondayDiscount); System.Console.WriteLine('%'); System.Console.WriteLine("========================"); System.Console.ReadKey(); } }
- Execute the program. This would produce:
-/- Georgetown Cleaning Services -/- ======================== Order Date: 7/15/2002 ------------------------ Item Type Qty Unit Price ------------------------ Shirts 5 0.95 Pants 2 1.95 Other Items 3 4.55 ------------------------ Monday Discount: 0.25% ========================
- Close the DOS window
Accessory Data Types
|
A string is an empty space, a character, a word, or
a group of words that you want the compiler to consider "as is", that
is, not to pay too much attention to what the string is made of, unless
you explicitly ask it to. This means that, in the strict sense, you can
put in a string anything you want.
Primarily, the value of a string starts with a
double quote and ends with a double-quote. An example of a string is
"Welcome to the World of C# Programming!". You can include such a string
in System.Console.Write() to display it on the console. Here is
an example:
class Exercise
{
static void Main()
{
System.Console.WriteLine("Welcome to the World of C# Programming!");
}
}
This would produce:
Welcome to the World of C# Programming! Press any key to continue . . .
Sometimes, you will need to use a string whose value
is not known in advance. Therefore, you can first declare a string
variable. To do this, use either the var or the string
keyword followed by a name for the variable. When declaring a string
variable, you can initialize it with an empty space, a character, a
symbol, a word, a group of words, or null (but you must
not use string?). If not using
null, the value given to a string must be included in
double-quotes. Here are two examples:
class Exercise
{
static void Main()
{
var team = "Real Madrid";
string country = "Guinée Equatoriale";
System.Console.WriteLine("Welcome to the World of C# Programming!");
System.Console.Write("Team: ");
System.Console.WriteLine(team);
System.Console.Write("Country: ");
System.Console.WriteLine(country);
System.Console.WriteLine();
}
}
This would produce:
Welcome to the World of C# Programming! Team: Real Madrid Country: Guinée Equatoriale Press any key to continue . .
.
|
- To use strings, change the file as follows:
class Order { static void Main() { string customerName; string customerHomePhone; byte? shirts = null; decimal? priceOneShirt = null; byte? pants = null; decimal? priceAPairOfPants = null; ushort? otherItems = null; decimal? priceOtherItems = null; uint? orderDay = null; uint? orderMonth = null; uint? orderYear = null; double? mondayDiscount = null; customerName = "Gregory Almas"; customerHomePhone = "(301) 723-4425"; shirts = 5; priceOneShirt = 0.95M; pants = 2; priceAPairOfPants = 1.95M; otherItems = 3; priceOtherItems = 4.55M; orderDay = 15; orderMonth = 7; orderYear = 2002; mondayDiscount = 0.25D; // 25% System.Console.WriteLine("-/- Georgetown Cleaning Services -/-"); System.Console.WriteLine("========================"); System.Console.Write("Customer: "); System.Console.WriteLine(customerName); System.Console.Write("Home Phone: "); System.Console.WriteLine(customerHomePhone); System.Console.Write("Order Date: "); System.Console.Write(orderMonth); System.Console.Write('/'); System.Console.Write(orderDay); System.Console.Write('/'); System.Console.WriteLine(orderYear); System.Console.WriteLine("------------------------"); System.Console.WriteLine("Item Type Qty Unit Price"); System.Console.WriteLine("------------------------"); System.Console.Write("Shirts "); System.Console.Write(shirts); System.Console.Write(" "); System.Console.WriteLine(priceOneShirt); System.Console.Write("Pants "); System.Console.Write(pants); System.Console.Write(" "); System.Console.WriteLine(priceAPairOfPants); System.Console.Write("Other Items "); System.Console.Write(otherItems); System.Console.Write(" "); System.Console.WriteLine(priceOtherItems); System.Console.WriteLine("------------------------"); System.Console.Write("Monday Discount: "); System.Console.Write(mondayDiscount); System.Console.WriteLine('%'); System.Console.WriteLine("========================"); System.Console.ReadKey(); } }
- Execute the program. This would produce:
-/- Georgetown Cleaning Services -/- ======================== Customer: Gregory Almas Home Phone: (301) 723-4425 Order Date: 7/15/2002 ------------------------ Item Type Qty Unit Price ------------------------ Shirts 5 0.95 Pants 2 1.95 Other Items 3 4.55 ------------------------ Monday Discount: 0.25% ========================
- Close the DOS window
A date is a unit that measures the number of years,
months, or days elapsed in a specific period. A time is a unit that
counts the number of seconds that have elapsed since midnight of the day
considered. Although dates and times are large subjects that would
require a detailed study, at this time, we will consider them in simple
terms.
To declare a variable that would hold date or time
values, use the DateTime data type.
class Exercise
{
static void Main()
{
DateTime DateHired;
}
}
The .NET Framework sets its starting periodic date
to January 1, 0001 at midnight (12:00:00 or 0:00 AM). If not assigned a
specific value (in future lessons, we will learn that this is equivalent
to declaring a variable using the default constructor), the variable is
initialized to 1/1/0001 at midnight.
The object data type is used to declare a
variable whose type is not primarily defined and can be any of the other
data types we have introduced. After creating an object variable, you
can use its value as you see fit. For example, you can enter the
variable in the parentheses of System.Console.Write() or
System.Console.WriteLine() to display it in the console window. Here
is an example:
class Exercise
{
static void Main()
{
var EmployeeName = "Ernestine Lamb";
object Address = "10244 Lockwood Drive";
System.Console.Write("Employee Name: ");
System.Console.WriteLine(EmployeeName);
System.Console.Write("Home Address: ");
System.Console.WriteLine(Address);
System.Console.WriteLine();
}
}
This would produce:
Employee Name: Ernestine Lamb Home Address: 10244 Lockwood Drive Press any key to continue . . .
A variable declared with object can embrace
almost any value. This means that, when initializing the variable, you
can use any of the types of values we have seen so far. Here are
examples:
class Program
{
static void Main()
{
object propertyNumber = "293749";
object propertyType = 'S';
object stories = 3;
object bedrooms = 4;
object value = 425880;
System.Console.WriteLine("=//= Altair Realtors =//=");
System.Console.WriteLine("Properties Inventory");
System.Console.Write("Property #: ");
System.Console.WriteLine(propertyNumber);
System.Console.Write("Property Type: ");
System.Console.WriteLine(propertyType);
System.Console.Write("Stories: ");
System.Console.WriteLine(stories);
System.Console.Write("Bedrooms: ");
System.Console.WriteLine(bedrooms);
System.Console.Write("Market Value: ");
System.Console.WriteLine(value);
}
}
This would produce:
=//= Altair Realtors =//= Properties Inventory Property #: 293749 Property Type: S Stories: 3 Bedrooms: 4 Market Value: 425880 Press any key to continue . . .
Custom Constants
|
Suppose you intend to use a number such as 39.37
over and over again. Here is an example:
class Exercise
{
static void Main()
{
double meter, inch;
meter = 12.52D;
inch = Meter * 39.37D;
System.Console.Write(meter);
System.Console.Write("m = ");
System.Console.Write(inch);
System.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:
class Exercise { static void Main() { double meter, inch; meter = 12.52D; inch = Meter * 39.37; System.Console.Write(meter); System.Console.Write("m = "); System.Console.Write(inch); System.Console.WriteLine("in\n"); meter = 12.52D; Inch = Meter * 3.937; System.Console.Write(meter); System.Console.Write("m = "); System.Console.Write(inch); System.Console.WriteLine("in\n"); meter = 12.52D; Inch = Meter * 393.7; System.Console.Write(meter); System.Console.Write("m = "); System.Console.Write(inch); System.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.37D;
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:
class Exercise { static void Main() { const double conversionFactor = 39.37D; double meter, inch; meter = 12.52D; inch = Meter * ConversionFactor; System.Console.Write(meter); System.Console.Write("m = "); System.Console.Write(inch); System.Console.WriteLine("in\n"); meter = 12.52D; inch = Meter * ConversionFactor; System.Console.Write(meter); System.Console.Write("m = "); System.Console.Write(inch); System.Console.WriteLine("in\n"); meter = 12.52D; inch = Meter * ConversionFactor; System.Console.Write(meter); System.Console.Write("m = "); System.Console.Write(inch); System.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 an 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.
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 (normally, those
constant are defined in the System namespace; other constant are
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.
The primary means of creating a program consists of
adding code to a document. This is done by typing code using the
keyboard. Once the code exists, you can do various things on it. In most
cases, you must first select the text. Normally, from your experience of
using computers, you should already know how to select text, using the
mouse, or a combination of the mouse and keyboard.
Accessing a Variable
|
As mentioned already, you will write your code in
normal text editors, whether using Notepad, the Code Editor of Microsoft
Visual Studio, or else. Also, you may be familiar already with how to
look for a character, a symbol, a word, or a group of words in a
document. Just as reminder, on the main menu of the application, you can
click Edit -> Find...
This would display a dialog box where you can type
the item and click Find. If you are using Microsoft Visual Studio and if
you want to find different occurrences of a known character, symbol,
word, or group of words, first select that item. Then:
- On the main menu, click Edit -> Quick Find
- Press Ctrl + F
In the same way, if you have a variable that is used
more than once in your code and you want to see all places where that
variable is used, simply click the name (and wait two seconds) and all
of its occurrences would be highlighted:
To get a list of all sections where the variable is
used, if you are using Microsoft Visual Studio:
- In the Code Editor, right-click the name of the variable and click Find All References
- Press Ctrl + F12
This would produce a list of all sections where the
variable is used and would display the list in the Find Symbol
Results window:
To access a particular section where the variable is
used, double-click its entry in the list.
Normally, from your knowledge of using computers,
you probably already know how to select, cut, and copy text. These two
operations can be valuable to save code in Microsoft Visual Studio. This
means that, if you have code you want to use in different sections, you
can preserve it somewhere to access it whenever necessary.
To save code to use over and over again, first type
the code in any text editor, whether in Notepad, Microsoft Word, or the
Code Editor of Microsoft Visual Studio. You can use code from any
document where text can be copied, including a web page. Select that
code and copy it to the clipboard. To preserve it, in Microsoft Visual
Studio, display the Toolbox (on the main menu, you can click View ->
Toolbox). Right-click an empty area on the Toolbox and click Paste:
An alternative is to select the code, whether in the
Code Editor or in a text editor. Then drag it and drop it on the
Toolbox. In the same way, you can add different code items to the
Toolbox. After pasting or adding the code to the Toolbox, it becomes
available. To use that code, drag it from the Toolbox and drop it in the
section of the Code Editor where you want to use it.
Renaming a Variable
|
As we will see throughout our lessons, there are
many names you will use in your programs. After creating a name, in some
cases you will have to change it. You can find where the name is and
edit it. If the name is used in many places, you can continue looking
for it and modify it. There is a chance you will make a mistake. If you
are writing your code using a text editor, you can use the Edit ->
Replace option of the main menu to find and replace every instance of
that name. You can use the same approach in the Code Editor.
Unfortunately, this technique works for only one file. If your project
has many files and the name is used in those files, it would be
cumbersome to remember to change the name in all of them.
Microsoft Visual Studio makes it easy to find and
change a name wherever it is used. Consider the following code:
class Order { static void Main() { int nbr = 148; System.Console.WriteLine(nbr); } }
To change the name of a variable, in the Code
Editor:
- Double-click the name of the variable and edit (change) it. The
name will then have a small underline:
If you position your mouse on it, a tag would appear and you can click the arrow to reveal a short menu:
If you click the first option, all instances of the variable would be changed. If you want to display a preview window (next), click the second option - Right-click it, position the mouse on Refactor, and click Rename...
This would display the Rename dialog box with the
primary name. If you want to change it, type the new name:
If you want the studio to find and change the name
inside the comments, click the Search in Comments check box. If the name
is used in strings and you want to replace it, click the Search in
Strings check box. When you click OK, the Preview Changes - Rename
dialog box would come up. It shows the various parts where the name is
used and will be replaced:
If you still want to replace, click Apply.
Accessing a Variable's Declaration
|
If you create a long document that has many lines of
code, in a certain section you may encounter a variable but you want to
find out where it was declared. If you are using Microsoft Visual
Studio, to access the place where a variable was declared:
- Right-click the variable and click Go To Definition...
- Click the name of the variable and press Shift + F2
In both cases, the caret would jump to where the
variable was declared.
Accessing a Line of Code by its Index
|
If you are using the Code Editor of Microsoft Visual
Studio, if you create a long document that has many lines of code, if
you want to jump to a certain line of code:
- On the main menu, click Edit -> Go To...
- Press Ctrl + G
This would display a dialog box. Enter the line
number and click OK or press Enter.
|
- Close your programming environment
- When asked whether you want to save, click No
No comments:
Post a Comment