0

I ask the Inference Engine a question as follows, and want to obtain the first Fact, for the first Query (the question consists of 2 Atoms in the Query), if a result is returned.

strAnswer = IIf(InfEng.RunQuery(Question).Count > 0, InfEng.RunQuery(Question)(0)(0).ToString, "No Answer")

This throws an error

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. 
Must be non-negative and less than the size of the collection.Parameter name: index"
when the Query count = 0. In the 'Watch' window, it evaluates to 0, and has the message: Error & No children, when I open up the evaluated statement. When the query returns Facts, this works without a hitch.

I substituted .Any, instead of Count, but that also did not work.

When I changed this to a standard If-Then-End If, it works in all cases...and with .Count & .Any

What am I doing incorrectly ?

RTK
  • 15
  • 3

1 Answers1

0

I'm no VB.NET expert but the fact InfEng.RunQuery(Question) appears twice in the IIf statement means the query will be executed twice if .Count > 0. You don't want this to happen, ie running a query twice for no good reason.

I suggest you assign InfEng.RunQuery(Question) to a variable then use this variable in the IIf statement.

David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • Useful tip / good catch. Thanks a lot. Can't vote up, since I don't have the reputation(15) as yet. – RTK Jun 23 '13 at 10:22