The Part of Tens - Pdf 63

Part VI
The Part of Tens
24_597043 pt06.qxd 9/20/05 2:20 PM Page 365
In this part . . .
W
hat For Dummies book would be complete without
a Part of Tens? C# is great at finding errors in your
programs when you try to build them — you’ve probably
noticed that. However, the error messages it generates
can be cryptic — you’ve probably noted that, as well.
Chapter 16 reviews the ten most common build-time error
messages and what they most likely mean. Knowledge is
power, so Chapter 16 also suggests fixes for the problems
that are being reported. These items have been updated
to reflect a few changes in C# 2.0.
Many readers will have come to C# through the most
common of all object-oriented languages, C++. Chapter 17
quickly reviews the differences between the two languages,
including the differences between C# generics and C++
templates.
24_597043 pt06.qxd 9/20/05 2:20 PM Page 366
Chapter 16
The 10 Most Common Build Errors
(And How to Fix Them)
In This Chapter

The name ‘memberName’ does not exist in the class or namespace ‘className’

Cannot implicitly convert type ‘x’ into ‘y’

‘className.memberName’ is inaccessible due to its protection level

className
.
Finally, C# doesn’t simply spit out the name of the class. It prefers to tack on
the full namespace name — just in case the entire error message would have
been visible without scrolling over to your neighbor’s house.
25_597043 ch16.qxd 9/20/05 2:21 PM Page 367
The name ‘memberName’ does not exist in
the class or namespace ‘className’
This error message could mean that you forgot to declare a variable, as in the
following example:
for(index = 0; index < 10; index++)
{
// . . . whatever . . .
}
The variable
index
is not defined anywhere. (See Chapter 3 for instructions
on declaring variables.) This example should have been written as follows:
for(int index = 0; index < 10; index++)
{
// . . . whatever . . .
}
The same applies to data members of a class. (See Chapter 6.)
A more likely possibility is that you misspelled a variable name. The following
is a good example:
class Student
{
public string sStudentName;
public int nID;
}

25_597043 ch16.qxd 9/20/05 2:21 PM Page 368
Less popular but still way up on the Top 10 playlist is the possibility that the
variable was declared in a different scope, as follows:
class MyClass
{
static public void AverageInput()
{
int nSum = 0;
int nCount = 0;
while(true)
{
// read in a number
string s = Console.ReadLine();
int n = Int32.Parse(s);
// quit when the user enters a negative number
if (n < 0)
{
break;
}
// accumulate the value entered
nSum += n;
nCount++;
}
// now output the results
Console.WriteLine(“The total is “ + nSum);
Console.WriteLine(“The average is “ + nSum / nCount);
// this generates a build time error message
Console.WriteLine(“The terminating value was “ + s);
}
}

be lost — most notably, any fractional value that the
double
may possess.
369
Chapter 16: The 10 Most Common Build Errors (And How to Fix Them)
25_597043 ch16.qxd 9/20/05 2:21 PM Page 369


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status