469,356 Members | 2,168 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

DirectCast - Why is this not working?

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

Jan 5 '06 #1
4 1473
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

Jan 5 '06 #2
<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


Jan 5 '06 #3
"Vagabond Software" <vagabondsw-X-@-X-gmail.com> schrieb:
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?


Either use 'CType' or 'Double.Parse'. I don't think it matters whether or
not 'CType' is CLS-compliant or not because it is used inside the
implementation. DirectCast doesn't work with 'Double' and 'String' because
'String' is not a derived of 'Double' and 'DirectCast' is not used to unbox
a value type in the sample you gave.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jan 6 '06 #4
Vagebond,

Casting is not converting. That in the C deriving languages world the same
word is used for that is not relevant.

A value is a byte representation on the stack. An object is a schematic
representation depending on its type(class) somewhere in memory. A value can
not be placed in an other representation without converting it.

Cor
Jan 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Tubs | last post: by
13 posts views Thread by Crirus | last post: by
6 posts views Thread by Ot | last post: by
6 posts views Thread by Mark Nethercott | last post: by
7 posts views Thread by Brian Henry | last post: by
1 post views Thread by iwdu15 | last post: by
3 posts views Thread by =?Utf-8?B?TWlrZQ==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.