class operation
{
public void add(int val1 , int val2)
{
int result;
result= val1+ val2;
return result;
}
}
class view : operation
{
public override in add(int val1 , int val2)
{
int result;
result = val1+val2;
return result;
}
}
Invalid token 'in' in class, struct, or interface member declaration
How can I remove this error from above code?
in
is a reserved keyword in C#. You probably want:
public override int add(int val1, int val2)
Also in order to be able to override some method in a derived class this method must be virtual in the base class:
public class operation
{
public virtual int add(int val1, int val2)
{
int result;
result = val1 + val2;
return result;
}
}
In the operation
base class you have declared the add
method with no return type and yet you are trying to return an integer.
Also there's no point in overriding a base method just to repeat the same code. You could simply invoke the base method in the overriden method:
public class view : operation
{
public override int add(int val1, int val2)
{
return base.add(val1, val2);
}
}
or simply:
public class view : operation
{
}
which would be equivalent because you are not modifying anything in this method.
Shouldn't in
be int
? And shouldn't the first add
return int
rather than void
?
精彩评论