473,287 Members | 1,564 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,287 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 1596
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.