1

I have the following code regarding to XML validation:

var settings = new XmlReaderSettings();
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

private static void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.WriteLine("\tWarning: Matching schema/DTD not found.  No validation occurred." + args.Message);
        else
            Console.WriteLine("\tValidation error: " + args.Message);
    }

So the point here is, I want to be able to check to see if settings.ValidationEventHandler has any event delegate added it to it or not (I understand it is a reference to a method), but the only way I can use it on the left side of a statement is before -= or +=. So how do I check if any ValidationCallBack had happened or not? I want to be able to set the return type to either false or true based on if any ValidationCallBack happened or not.

Harvey Lin
  • 724
  • 11
  • 25
  • _"do I check if any ValidationCallBack had happened or not?" -- see marked duplicate for answer to your stated question. As far as whether the event has actually been raised or not (i.e. _"if any ValidationCallback had happened"_), that seems like a completely different question, and easily solved by setting a flag in the method itself when it's called. _"I want to be able to set the return type"_ -- what return type? – Peter Duniho Apr 24 '17 at 23:10
  • "and easily solved by setting a flag in the method itself when it's called." Does it meant that I can pass in another parameter in the method as a flag? Can you please point me to an example please? Thanks! – Harvey Lin Apr 24 '17 at 23:29
  • _"I can pass in another parameter in the method as a flag?"_ -- no, not easily. But any class field accessible by the method can be used. You can declare a `static bool flag;` for the purpose, for example. – Peter Duniho Apr 24 '17 at 23:32
  • Thanks, just to be sure, when I said "I want to be able to set the return type", I was referring to the return type of the calling method, depend on if and ValidationCallback happened or not, it will change it's return value to either true or false. I want to be able to change that value based on if any ValidationCallback has happened or not. – Harvey Lin Apr 24 '17 at 23:35
  • _"I was referring to the return type of the calling method"_ -- what calling method? The only method in your question is the event handler. The most obvious "calling method" would be the one calling the event handler, which is the method that raises the event, in the class where the event is declared (i.e. `XmlReaderSettings`). What makes you think you can modify that method to take into account a returned value, even if you could return a value? Your description is making very little sense. You may want to post a new question that is better, but make sure it has a good [mcve]. See also [ask]. – Peter Duniho Apr 24 '17 at 23:39
  • Nothing really, I guess the right question should be "How do I modify ValidationCallback so that whenever it is being called, I can do something about it?" – Harvey Lin Apr 24 '17 at 23:47
  • Did you write method? What "something" do you want to do "about it" when it's called? If you wrote the method, just do that "something". If you didn't, then you need to subscribe your own method that you _did_ write to the event and do your "something" there. Again, there is so little context here, it's pretty much impossible to really understand what you're trying to do. And the comments (or even chat) isn't going to be the right way to fix that. You need to figure out how to compose your question in a more thoughtful, careful way. Again, the pages [mcve] and [ask] will help you in that quest – Peter Duniho Apr 24 '17 at 23:51
  • I found a solution that worked here, I think it gets it done by adding a flag: http://stackoverflow.com/a/14218049/6026863 – Harvey Lin Apr 25 '17 at 18:53

0 Answers0