Today, when I was writing code, I found a strange phenomenon: I clearly defined an attribute that can both get and set, however, when I need to call this attribute outside the class, I find that the property cannot be found in intelliisense, but the get_property and set_property methods are found. Strange :)
Let's take a look at the Code:
Public class usercontrolblock: extendedcontrol, ipageblock
{
// The other code
Public String sourcefile
{
Get
{
Return _ sourcefile;
}
Set
{
If (_ sourcefile! = Value)
{
_ Sourcefile = value;
_ Sourceloaded = false;
}
}
}
// The other code
}
The above Code has no syntax errors, and the existing code has been compiled and runs properly. Other attributes can be found normally, but this sourcefile attribute cannot be found, which is really strange! So where does this attribute go ?~~~
The list of members of this class in the Object Browser is also changed to two get/set methods, or the sourcefile attribute cannot be found:
I cannot explain it... By chance, I noticed that this class implements the ipageblock interface. The source code of this interface is as follows:
Public interface ipageblock
{
String sourcefile {Get;} // only getter are declared here.
Bool sourceloaded {Get ;}
Void loadsource ();
Control {Get ;}
}
After reading the declaration about the sourcefile attribute in the interface, I suddenly realized! In the past, the attribute is split into two functions mainly because the interface's declaration of the attribute accessors is inconsistent with the definition of the accessors for the corresponding attributes in the implementation class :)
Find the reason. Here is a brief summary:When the number of accessors corresponding to the attribute in the implementation class is greater than the number of accessors declared in the interface, although there are some inconsistencies, the C # syntax is allowed at this time, it is not considered as an error. The compilation is successful. However, the original intuitive attribute access syntax will be split into two methods starting with get _ and SET _. You should be careful later :)
I do not know whether this problem exists in C #2.0? If you are interested, please try it and let us know. Thank you!