开发者

Invalid token 'in' in class, struct, or interface member declaration [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area,开发者_StackOverflow a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.
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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜