[Mono-list] XPathNavigator.Evaluate casting
Atsushi Eno
ginga@kit.hi-ho.ne.jp
Wed, 17 Sep 2003 22:27:22 +0900
Hello,
> XPathExpression xpCountExpr =
> m_vocabulary.Compile("count(//d-class[@id='label']/d-property[@id='constructor']/d-param)");
> int nrp = (int)m_vocabulary.Evaluate(xpCountExpr);
>
> But that did not work: I got a castexception in the line following on
> the "int npr" assignment. (Other similar pieces of code had the same
> effect).
This is the expected result. Because
1) XPathNavigator.Evaluate() with such XPath those result is
a number is evaluated as to return System.Double, but
2) the return type of XPathNavigator.Evaluate() is System.Object,
that is, the return value is boxed.
3) The code "(int)" means that it is unboxing the result of
"m_vocabulary.Evaluate(...)" to System.Int32, while it is
actually System.Double (unboxing does not automatically
calls subsequent typecast code).
Both typecasting and unboxing have the same syntax in C#.
Atsushi Eno