1

Get error 'ClassLibrary3.Class1.a' is a 'property' but is used like a 'type' when i want to access the property of base class.

 namespace ClassLibrary3
    {
        public class Class1
        {
            public int a { get; set; }
            public int A { get; set; }
        }

    public class test : Class1
    {
        a=1; // error 'ClassLibrary3.Class1.a' is a 'property' but is used like a 'type'
        public void hello()
        {
            a = 10;
        }
    }
}

3 Answers 3

3

Yes; that is normal. With the exception of field initializers, the code must be in a method. That isn't a field initializer.

Either assign a=1; in the derived constructor, or create an overloaded constructor and pass 1 to the base type's constructor.

So either:

public test() {
    a = 1;
}

Or:

protected Class1(int a) {
    this.a = a;
}
...
public test() : base(1)
{}
2

The error is because a=1 isn't in the body of a method.

0

If you want a to be equal to 1 when you create a test object, then you should put the a=1 into a constructor for the test class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.