473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1599
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andreas Klemt | last post by:
Hello, what has the better performance and what are you using? Dim myObj As Object = 70 a) Dim myInt As Integer = DirectCast(myObj, Integer) b) Dim myInt As Integer = Convert.ToInt32(myObj) ...
11
by: Tubs | last post by:
i am attempting to write something which can morph itself to whatever comes in and get the value property from it but i don't know what type it is until runtime. I am therefore trying to use...
13
by: Crirus | last post by:
Can I use DirectCast to convert a object to it's base or should I use CType? I have a instance of a class as Object. The run-time tipe is a derived of a class, but I need to refer to that instance...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
5
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
6
by: Mark Nethercott | last post by:
I get the following failure when trying to access the builtin properties; An unhandled exception of type 'System.InvalidCastException' occurred in resultsoutput.dll Additional information:...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
1
by: iwdu15 | last post by:
can anyone explain the directcast code...ive tried using it and lookin it up but im lookin for an easy definition and how it works...ive tried using it before byut it throws errors saying it can...
3
by: =?Utf-8?B?TWlrZQ==?= | last post by:
If Visual Studio knows the type, why does the system-generated property use CType instead of DirectCast? DirectCast is more efficient right? For example - Here's what we have for a setting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.