newbie: Converting DOUBLE to STRING

G

Guest

Hello -

Using C#, i am trying to convert a double value to string, so that it can be
assigned to a LABEL control on my Web Form Page. However, i continue to get
"invalid cast" errors. My code is as follows:

double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

I would have expected that the string "0.5" would be the result. Obviously,
i don't konw something basic. If someone could point out what i'm doing
wrong, that would be great.

Thanks,
 
G

Gabriel Lozano-Morán

double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
G

Guest

This is an instance of a standard textbox webcontrol. For example:

protected System.Web.UI.WebControls.TextBox txtResult;
 
G

Guest

Unfortunately, this does not work either. Hence, why i've written out the
"System.Convert.ToString"... to try and "force" the cast.
 
J

Juan T. Llibre

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------
 
G

Gabriel Lozano-Morán

;) I didn't even notice that :)

G.

Juan T. Llibre said:
re:

That *does* work...if you change .text to .Text

Not :

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
 
G

Guest

Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
 
G

Guest

To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
Gabriel Lozano-Morán said:
;) I didn't even notice that :)

G.
 
J

Juan T. Llibre

re:
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

Why are you using a LinkButton and not a Label there ?
A LinkButton control is a submit button.

Why do you want a result placed on a Submit Button ?

Aren't you declaring your Label like this :

Label lblResult = (Label)e.Item.FindControl("lblResult");

?

I haven't checked this out, but try this :

((Label)e.Item.FindControl(lblResult)).Text = iR.ToString();





charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
Gabriel Lozano-Morán said:
;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------





double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
H

Hans Kesting

charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

does FindControl really find a LinkButton?
try splitting the statement:
string s = iR.ToString();
LinkButton lb = (LinkButton)e.Item.FindControl(lblResult);
lb.Text = s;
and see where the error is.

Hans Kesting

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET
would not allow this to compile, so this is not the problem,
although, i my sample code below, this was indeed correct.

The *real* code is:

double iS =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried
this with manual values, following the sample below, and i still get
errors.... The error generated is:

A first chance exception of type 'System.InvalidCastException'
occurred in marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
Gabriel Lozano-Morán said:
;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server"
Text=""></asp:Label> </div>
</form>
</body>
</html>
---------





message double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult?
Instance of TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
G

Guest

Guys,

Very sorry for the mix-up... the problem was the (LinkButton) cast, not the
double. It should be a (TextBox) cast.

Again, thanks for your help!

charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
Gabriel Lozano-Morán said:
;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------





double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
J

Juan T. Llibre

Hmmm.. my hunch was on the right track.

I'll only add that, if it's a Textbox you are using,
you should change its identifier to txt, instead of lbl :

((Texbox)e.Item.FindControl(txtResult)).Text = iR.ToString();

....as opposed to :
((Label)e.Item.FindControl(lblResult)).Text = iR.ToString();






charliewest said:
Guys,

Very sorry for the mix-up... the problem was the (LinkButton) cast, not the
double. It should be a (TextBox) cast.

Again, thanks for your help!

charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
:

;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------





double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
G

Guest

Yes, all good ideas. Thanks Juan.

Juan T. Llibre said:
Hmmm.. my hunch was on the right track.

I'll only add that, if it's a Textbox you are using,
you should change its identifier to txt, instead of lbl :

((Texbox)e.Item.FindControl(txtResult)).Text = iR.ToString();

....as opposed to :
((Label)e.Item.FindControl(lblResult)).Text = iR.ToString();






charliewest said:
Guys,

Very sorry for the mix-up... the problem was the (LinkButton) cast, not the
double. It should be a (TextBox) cast.

Again, thanks for your help!

charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

:

Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
:

;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------





double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
M

Momcilo Majic

double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

The more efective way is to use implicit conversion which is triggered
by the operator's '+' side effect:

double r = 5/10;
txtResult.Text = "" + r;

the last line will implicitly call ToString method of the r object.

it looks much nicer, furthermore, in cases where object tipe is not
known, it is possible to avoid explicit reflection by using same trick:
double r=5/10;
object rr=r;

txtResult.Text = "" + rr;


:) it's nice to see that old tricks from Java are still working under c#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top