Click here to Skip to main content
14,739,784 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a follow on from another question, similar but not the same
why-I-need-to-parse-if-object-value-is-decimal


Taking a vb.net app, implicit conversion set to error in the compiler.

So - e.g.
Dim MyDecimal as Decimal=decimal.parse(0.001.tostring)

MyDecimal=MyDecimal*0.45355

Error: Implicit Conversion from Double to Decimal.


Ok. So then I have to do something strange like:

MyDecimal=MyDecimal*decimal.parse(0.45355.tostring)

Why does it have to be done this way - or is there a better way to manage this. Seems like a waste of a cpu cycle to convert it to a string, then to parse it as a decimal. Why cant the system just understand (i) that 0.45355 is decimal, because it is and (ii) why cant just decimal.parse(0.45355) work anyway if it doesnt understand that 0.45355 is a decimal.
Even if the number has less decimal points i.e. 0.4 it still moans that its a double.

Im just trying to understand the logic behind why the compiler thinks this? Thanks!

What I have tried:

Guess I have to fill this in just for the query to be accepted, but trying different decimal point seems to have no effect. if double "is the standard" for decimal, then why have decimal at all?
Posted
Updated 18-Feb-20 22:28pm
v3
Comments
[no name] 19-Feb-20 3:24am
   
How is about this:
Dim dec As Decimal
dec = 2.375D
Member 12561559 19-Feb-20 10:00am
   
Thanks!

Because you don't specify it, the constant literal 0.45355 is an implicit Double value, not a decimal - and since they take precedence over Decimal in calculating (so as not to lose precision) the result of Decimal * Double is Double.

Append a "D" to the end of your literal value and the system will know it's a Decimal value and act accordingly:
MyDecimal = MyDecimal * 0.45355D
   
Comments
Member 12561559 19-Feb-20 3:42am
   
Ok, that makes sense to be honest. So again, as Im trying to implement the parse for most things as convert.to is outdated from a previous question I supplied that someone mentioned - I have a date:

Dim CleanDaysToGo as int32=0
CleanDaysToGo = NextCleanDate.Value.Subtract(Now).TotalDays
doesn't work because of datetime issues, so utilising the parse.datetime and parse.int32 to turn the result into an int32 on the outer, like so:


Dim CleanDaysToGo as int32=0
CleanDaysToGo = Int32.Parse(DateTime.Parse(NextCleanDate.Value.ToString).Subtract(Now).TotalDays.ToString)

NextCleanDate is a of nullable datetime equal to say, 25 february 2020 - so 6 days from now - and CleanDaysToGo should hold that value - but it flops with an error, only on the runtime with "input string is not in correct format".

Sorry to seem so dumb with this and following up a question, with a question, but it might be useful for others who run into the same issues. :) Cheers
Richard MacCutchan 19-Feb-20 3:55am
   
You need to read the documentation on types and classes. You appear to be under the misapprehension that the compiler can guess your intentions.
The Decimal constructor has an overload for Double. See Decimal Constructor (System) | Microsoft Docs[^].
   
Comments
Member 12561559 19-Feb-20 3:43am
   
Thanks! sorry I could not give you the accept solution, just griff got in there first - otherwise, 5 stars from me :)
CPallini 19-Feb-20 4:04am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900