from static function we can create a instance but we can not access any non-static data member….why.
public static DropoffType DROP_BOX
{
get
{
DropoffType tempType = new DropoffType();
tempType.DropoffTypeSelected = _DROP_BOX;
y=11; // compile time error
return tempType;
}
}
from the above code we can see that instance can be created from property but when we are trying to access any non-static data member then i am getting complile time error. can any one explain the reason in detail that why instance can be created from any where but non-static data member can’t be access from static function.
If
yis an instance field, you can’t access it without telling it which object you mean. For an instance member, there is an implicitthis., i.e.this.y.Just add:
The problem is, you could have any number of variables and objects kicking around. And even for an instance method you often have to disambiguate, i.e.
(in the above the
this.is not strictly needed, but helps the reader IMO; theother.is entirely necessary)