<docs>
DirectCast requires an inheritance or implementation relationship between
the data types of the two arguments. This means that one type must inherit
from or implement the other.
DirectCast generates a compiler error if it detects that no inheritance or
implementation relationship exists. But the lack of a compiler error does
not guarantee a successful conversion. If the desired conversion is
narrowing, it could fail at run time. If this happens, the runtime throws an
InvalidCastException error.
</docs>
"String" does not inherit or implement "Double" (or vice versa for that
matter) and therefore fails.
--
Colin Neller
http://www.colinneller.com/blog
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:ea*************@TK2MSFTNGP09.phx.gbl...
Why not use Double.Parse(strOrderTotal) ?
AFAIK CType is CLS compliant. It is very hard to code in .NET without it.
:)
Greg
"Vagabond Software" <vagabondsw-X-@-X-gmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl... Here is the code:
Dim amountDue As Double
amountDue = Math.Round(DirectCast(strOrderTotal, Double), 2)
This code produces an invalid cast exception. The String variable
strOrderTotal evaluates to "226.27".
Is it not valid to cast a string to a double? I don't want to use CType
because it is my understanding that CType is not CLS compliant. Is that
correct?
carl