I'm trying to do this:
Double myDouble
myDouble = (double) myString
I have ensured that myString has a valid value i.e. 79.46. But the compiler
won't let me do the cast.
How can I get the value into myDouble.
Thanks,
T 11 2550
Try this:
myDouble = Double.Parse();
Make sure to handle any possible errors of course. look it up in
documention.
"Tina" <ti**********@nospammeexcite.com> wrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl... I'm trying to do this: Double myDouble myDouble = (double) myString
I have ensured that myString has a valid value i.e. 79.46. But the compiler won't let me do the cast.
How can I get the value into myDouble.
Thanks,
T
prefer the use of double.tryparse instead because 1. it doesn't throw
2. it is culture aware
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------
"Glen Martin" <He*********@newsgroups.nospam> wrote in message
news:uC**************@TK2MSFTNGP04.phx.gbl... Try this:
myDouble = Double.Parse();
Make sure to handle any possible errors of course. look it up in documention.
"Tina" <ti**********@nospammeexcite.com> wrote in message news:%2******************@TK2MSFTNGP02.phx.gbl... I'm trying to do this: Double myDouble myDouble = (double) myString
I have ensured that myString has a valid value i.e. 79.46. But the compiler won't let me do the cast.
How can I get the value into myDouble.
Thanks,
T
Try this
System.String MyString = "123.456";
System.Double MyDouble;
System.Boolean CanBeConverted = System.Double.TryParse(MyString, out
MyDouble);
"Tina" <ti**********@nospammeexcite.com> a écrit dans le message de news:
%2******************@TK2MSFTNGP02.phx.gbl... I'm trying to do this: Double myDouble myDouble = (double) myString
I have ensured that myString has a valid value i.e. 79.46. But the compiler won't let me do the cast.
How can I get the value into myDouble.
Thanks,
T
<"Alvin Bruney" <www.lulu.com/owc>> wrote: prefer the use of double.tryparse instead because 1. it doesn't throw
Well, that depends on whether or not you *want* it to throw an
exception if the value is invalid. On several occasions it makes
perfect sense to throw.
2. it is culture aware
Double.Parse is culture-aware as well. You can pass it an
IFormatProvider if you want to, but if you use an overload which
doesn't take one, and the culture associated with the current thread.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Yup, that's correct. I think the Double.Parse method evolved while I wasn't
looking from one version of the framework to the other. It now seems that
the only benefit of the tryparse over parse is that it doesn't throw.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... <"Alvin Bruney" <www.lulu.com/owc>> wrote: prefer the use of double.tryparse instead because 1. it doesn't throw
Well, that depends on whether or not you *want* it to throw an exception if the value is invalid. On several occasions it makes perfect sense to throw.
2. it is culture aware
Double.Parse is culture-aware as well. You can pass it an IFormatProvider if you want to, but if you use an overload which doesn't take one, and the culture associated with the current thread.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
> looking from one version of the framework to the other. It now seems that the only benefit of the tryparse over parse is that it doesn't throw.
That's no benefit.
PIEBALD <PI*****@discussions.microsoft.com> wrote: looking from one version of the framework to the other. It now seems that the only benefit of the tryparse over parse is that it doesn't throw.
That's no benefit.
Well, it is in some circumstances - but it entirely depends on the
situation.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
> > > looking from one version of the framework to the other. It now seems
that the only benefit of the tryparse over parse is that it doesn't throw.
That's no benefit.
I look at it as, "the benefit of Parse is that it _will_ throw when there's
a problem".
When writing a class that implements a .Parse method the developer can
choose to throw different exceptions for different problems and the user of
the class can then choose an appropriate action based on the problem. The
more information the better.
PIEBALD <PI*****@discussions.microsoft.com> wrote: I look at it as, "the benefit of Parse is that it _will_ throw when there's a problem".
When writing a class that implements a .Parse method the developer can choose to throw different exceptions for different problems and the user of the class can then choose an appropriate action based on the problem. The more information the better.
Unless all you're really interested in is "has the user entered a valid
number". That's pretty common when validating input, and having to use
a try/catch for it is ugly.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Ugly, schmugly, who's going to see it? And it's good practice for the kiddies.
PIEBALD <PI*****@discussions.microsoft.com> wrote: Ugly, schmugly, who's going to see it?
Whoever's maintaining the code.
And it's good practice for the kiddies.
???
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian Blais |
last post by:
Hello,
I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like:
d=(double *) malloc(50*sizeof(double));
why is this bad? I had always thought...
|
by: Francois Malgreve |
last post by:
in the following code:
object obj = this.ViewState;
string s = obj.GetType().ToString();
currentCreditLimit = (double) this.ViewState;
ViewState is a StateBag object (dictionary type object)...
|
by: René Nordby |
last post by:
Hi there,
Is there anyone that knows how to do the following?
I have a class A and a class B, that 100% inherits from class A (this means
that I don't have other code in class B, than...
|
by: Herby |
last post by:
Given class B and C which inherit from class A
They all override a method of the form:
Add( A^ lhs, A^ rhs ); So A is abstract.
So if i was defining Add for class B :
B::Add( A^ lhs, A^ rhs...
|
by: dragoncoder |
last post by:
Consider the code
class A {
private:
int a;
};
int main(void) {
A x;
int* ptr = (int*)&x;
|
by: g3000 |
last post by:
I have a problem with a Visual Studio 2005 web project.
I have two pages. SelectProject.aspx and ShowProject.aspx
The first page ( SelectProject.aspx) has two drop down lists.
After the user...
|
by: Giulio Petrucci |
last post by:
Hi everybody,
here's my problem: I have to dymanically build (and compile, of course)
some code, from some ECMAScript function. ECMAScript variables I get are
not typezed, so I should have...
|
by: Beta What |
last post by:
Hello,
I have a question about casting a function pointer. Say I want to make
a generic module (say some ADT implementation) that requires a function
pointer from the 'actual/other modules'...
|
by: kanepart2 |
last post by:
Hi guys, I am having a problem with the following code snippet:-
double x = (myReader);
double y = (myReader);
Resulting in the follwing compilation error:
Cannot implicitly convert type...
|
by: scottc |
last post by:
i'm stuck and i need a little direction. i'm only getting 2 error messages. i have 2 files:
import java.util.ArrayList;
import java.util.*;
//author
public class StudentTester
{
...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
| |