InvalidCastException "Cast from string "6<5" to type 'Boolean is not valid.

H

hazz

I get an InvalidCastException "Cast from string "6<5" to type 'Boolean is
not valid.

for

If (CInt(objBuyInfo.Budget) & " " & arrRuleList(i).operator & " "
& CInt(arrRuleList(i).LeadMemberValue)) Then

where arrRuleList(i).operator = ">"

and CInt(objBuyInfo.Budget) = 6

and CInt(arrRuleList(i).LeadMemberValue) = 5



The quickwatch shows "6 < 5" {String} Object


How can I create a legitimate expression from this property values?

Thank you,
-hazz
 
M

Marina

You can't. You need to evaluate a string as if it were code, and there is
no easy way to do that. You would need to either parse it out yourself, or
there are ways you can write, compile and execute code at runtime (slow and
not so pretty).

In your case, I am not sure why you are turning things into Integers, just
to then turn them back into a string during concatentation?

The easiest way to do this in your particular case would be to write a
function that took as parameters the values to compare, and the operator.
Then just have a case statement on the operator, and perform the appropriate
comparison with the operands. Return the result of the comparison.

Also, you should turn Option Strict On. That would prevent these errors at
compile time, rather then at runtime. You should always keep this option
turned on.
 
H

hazz

Thank you for your quick reply Marina!
Thank you for the following suggestion which I was beginning to see as the
only solution.

And thank you for your other comments. Option Strict On is a fixture now.

Appreciatively,
-hazz
 
A

Armin Zingler

hazz said:
I get an InvalidCastException "Cast from string "6<5" to type 'Boolean is
not valid.

for

If (CInt(objBuyInfo.Budget) & " " & arrRuleList(i).operator & " "
& CInt(arrRuleList(i).LeadMemberValue)) Then

where arrRuleList(i).operator = ">"

and CInt(objBuyInfo.Budget) = 6

and CInt(arrRuleList(i).LeadMemberValue) = 5



The quickwatch shows "6 < 5" {String} Object


How can I create a legitimate expression from this property values?


http://msdn.microsoft.com/library/e...gsourcecodedynamicallyinmultiplelanguages.asp


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top