Home All Groups Group Topic Archive Search About

DirectCast - Why is this not working?

Author
5 Jan 2006 11:03 PM
Vagabond Software
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

Author
5 Jan 2006 11:17 PM
Greg Burns
Why not use Double.Parse(strOrderTotal) ?

AFAIK CType is CLS compliant.  It is very hard to code in .NET without it.
:)

Greg

Show quoteHide quote
"Vagabond Software" <vagabondsw***@-X-gmail.com> wrote in message
news:%23fHi5wkEGHA.532@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
>
>
>
Author
5 Jan 2006 11:28 PM
Colin Neller
<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.

Show quoteHide quote
"Greg Burns" <bluebunny@newsgroups.nospam> wrote in message
news:eaDyv4kEGHA.312@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-gmail.com> wrote in message
> news:%23fHi5wkEGHA.532@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
>>
>>
>>
>
>
Author
6 Jan 2006 12:30 AM
Herfried K. Wagner [MVP]
"Vagabond Software" <vagabondsw***@-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/>
Author
6 Jan 2006 9:21 AM
Cor Ligthert [MVP]
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