Why is it not possible to assign a class Non-static Data Member Variable with another Variable? For ex:
class A {
bool firstFlag=false;
bool secondFlag=firstFlag; // Showing Error.
void SomeMethod(){}
}
Why is it not possible to assign a class Non-static Data Member Variable with another Variable? For ex:
class A {
bool firstFlag=false;
bool secondFlag=firstFlag; // Showing Error.
void SomeMethod(){}
}
Answer is OOPS :-)
firstFlag & secondFlag both are Class DataMembers. By definition you can only access them in Member Methods or Constructors.
To do what you need - try this:
internal class A
{
bool firstFlag = false, secondFlag = false;
}
Thanks to the discussion on : A field initializer cannot reference the nonstatic field, method, or property
Please refer The C# Language Specification Section 10.5.5.2 Instance Field Initialization states - A variable initializer for an instance field cannot reference the instance being created. Thus, it is a compile-time error to reference "this" in a variable initializer.( hence it is a compile-time error for a variable initializer to reference any instance member through a simple-name).