In our last lesson, we saw many of the most basic build-time errors in C#.
In this session, we will look at some of the errors related to:
- classes
- subclasses
- inheritance
Once we address some of the more common errors we will take a look at how you can fix them.
#1 Hidden Method Name Creates Overload
This conflict arises when a base class and its subclass have a function of the same name
public class MyBaseClass
{
public void Function()
{ // function code goes here
}
{
public class MySubClass : MyBaseClass
{
public void Function()
{ // function code goes here
}
}
public class YourClass
{
public void YourFunction()
{
SubClass ysb = new MySubClass;
ysb.Function();
}
}
The function YourFunction() cannot access the MyBaseClass.Function() from MySubClass because MySubClass.Function() hides it.
If the MySubClass.Function() is supposed to be hidden, the line that creates MySubClass.Function() should contain the keyword “new” to differentiate it from the MyBaseClass.Function():
public class MySubClass : MyBaseClass
{
new public void Function()
{ // function code goes here
}
}
If the MySubClass.Function() is supposed to be inherited polymorphically from MyBaseClass, the line that creates MySubClass.Function() should contain the keyword “override” and the MyBaseClass.Function() line should contain the keyword “virtual” to allow for the override:
public class MyBaseClass
{
public virtual void Function()
{ // function code goes here
}
}
public class MySubClass : MyBaseClass
{
public override void Function()
{ // function code goes here
}
}
#2 Cannot Inherit from Sealed Class, Method or Variable
Programmers typically seal classes to protect them from modification from inherited classes, so they do not define their classes that are expected to pass methods and variables through inheritance as sealed.
public class Xray
{
protected virtual void Function()
{
Console.WriteLine("Xray.Function");
}
protected virtual void Function2()
{
Console.WriteLine("Xray.Function2");
}
} // end of class Xray
public class Yankee : Xray
{
sealed protected override void Function()
{
Console.WriteLine("Xray.Function");
}
protected override void Function2()
{
Console.WriteLine("Xray.Function2");
}
} //end of class Yankee
public class Zulu : Yankee
{
protected override void Function()
{
Console.WriteLine("Yankee.Function"); // compiler error due to sealed function in Yankee
}
protected override void Function2()
{
Console.WriteLine("Yankee.Function2");
}
} // end of class Zulu
Since Yankee.Function() was defined as sealed, the attempt to override it in Zulu caused a compiler error.
The workaround for this would be to create a new instance of the class, which would “open” the seal on the function, rather than attempting to inherit the class.
#3 Class Does Not Implement a Method from an Interface
This error can be one of the most difficult to find, but one of the easiest to fix. When a class implements an interface, it must define all the methods in that interface and use the same variable types specified in the interface’s functions.
interface ICarly {
void functionSam(string strSam);
void functionSpencer(float fSpencer);
}
public class Freddie : ICarly {
public void functionSam(string strSam);
public void functionSpencer(int fSpencer);
}
Since the functionSpencer() in the class Freddie calls for an int variable instead of the float variable in the interface, the class will not implement functionSpencer(). A similar error occurs when constructing a concrete class that pulls methods from an abstract class without overriding the abstract methods.
Most build-time errors are relatively easy to repair after the programmer discovers the source. The key is to keep track of all the methods and variables for type, spelling, declaration and other basic qualities to prevent many of these simple mistakes.