I have a string property that defines a filename for an xml file. When the user inputs this filename into the property, I have the setter calling a parseXml() function开发者_JS百科 immediatly after setting 'fileName = value' to populate a dataTable with the data from the XML file so it displays in the designer. For some reason, when I have this function call in the property setter, the setter ends up getting called every twice every time I change the property, with the 2nd time being an empty string which causes an error. Why is it doing this?
public String FileName
{
get { return fileName; }
set
{
fileName = value;
parseXmlFile();
}
}
My initial guess would be that something in parseXml() is calling that setter again. What happens if you remove the call to parseXml()? Have you tried debugging and stepping through the code as it is running to see what exactly is calling the setter the second time?
If you slap a breakpoint on filename = value;
and hit it, what does the callstack window show you?
Short answer: it shouldn't. More helpful: maybe you cause the second call yourself? Set the debugger on the setter and the second time it is called, inspect the call stack.
As a complete aside to the problem you're having, putting expensive IO operations behind property setters is a little off kilter.
If you want to open up a file and parse stuff etc it would be better to have a separate method named appropriately that does the IO and sets this property (filename) at the end when the method successfully completed its work.
精彩评论