I have the Parent Class :
class clsTestParent
{
protected int x;
public void Foo()
{
x = 10;
}
}
I have the Derrived Class as below:
class clsDerivedTest : clsTestParent
{
x = 10;
Foo();
}
But this is not working as I am getting following two errors:
Invalid token ‘=’ in class, struct, or interface member declaration
Method must have a return type
But the above statements works fine when I try to use them with a method in derived class as below:
class clsDerivedTest : clsTestParent
{
public void myTestMethod()
{
x = 10;
Foo();
}
}
Why the protected var or methods are only accessible by using derived class methods but not direcly in the class.
I even tried accessing the Protected member by creating object as below:
clsDerivedTest objDerivedTest = new clsDerivedTest();
objDerivedTest.x = 10;
But again getting error for Protection Level. I have the var as protected so why the object of derived class can not access it?
I need to clear my fundamentals of OOPs but stuck here.
You know your problem –
I need to clear my fundamentals of OOPs but stuck here.
Clear the concept of protected member:
The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
Also, it will be good if you go through C# language specification. It will make your understanding clear.