Cannot implicity convert type ‘double’ to ‘string’

I am making a program to take the radius of a circle and output the diameter, area, and circumference. I’m trying to start with the diameter, but I keep receiving the error: Cannot implicitly convert type ‘double’ to ‘string.’ I’ve done similar programs using integers, but I can’t figure out for the life of me, how to receive floats in text boxes and calculate them so I can output them. This is my code so far.

<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
 protected void Button1_Click(object sender, EventArgs e)
 {
 double pi = 3.14159;
 lblDiameter.Text = (double.Parse(radius.Text)) * (double.Parse(radius.Text));
 } 
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title>Circles</title>
</head>

<body>
<form id="form1" runat="server">
    <div>
    <asp:TextBox
    id="radius"
     Runat="server" /> 
    <br /> 

    <asp:Button
    id="Button1"
    Text="Calculate"
    OnClick="Button1_Click" 
    Runat="server" />

    <asp:Label
    id="lblDiameter"
    Runat="server" />
</div>
</form>
</body>
</html>

Where am I going wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You’re probably just missing a .ToString():

lblDiameter.Text = (double.Parse(radius.Text) * double.Parse(radius.Text)).ToString();

It’d be clearer and you’d avoid parsing the string twice by storing the number in a local variable:

var value = double.Parse(radius.Text);
lblDiameter.Text = (value * value).ToString();

Now, is a diameter really equal to the square of the radius? 😉

Method 2

You are getting the error because you are dealing with double data type on the right side of the equation and a string on the left.

Change your code from:

lblDiameter.Text = (double.Parse(radius.Text)) * (double.Parse(radius.Text));

To:

lblDiameter.Text = Convert.ToString((double.Parse(radius.Text)) * (double.Parse(radius.Text)));

or:

lblDiameter.Text = (double.Parse(radius.Text) * (double.Parse(radius.Text)).ToString();

or:

double radius = double.Parse(textBox1.Text);
lblDiameter.Text = (radius * radius).ToString();

Also you do not need to set pi. There is a constant in the Math namespace.

double pi = Math.PI;

I also suggest set a radius variable to use over again instead of doing (double.Parse(radius.Text) each time. Like in my example above.

double radius = double.Parse(textBox1.Text);

Then you can do things like:

lblDiameter.Text = Convert.ToString(radius * radius);

and:

lblDiameter.Text = (radius * radius).ToString();

Also:

double circum = (2 * pi) * radius;

Method 3

On the line of code lblDiameter.Text = (double.Parse(radius.Text)) * (double.Parse(radius.Text));

The left hand side lblDiameter.Text is considered a string and the right hand side is a double, as you parsed the value of the textbox to a double.

Here’s what you should do:

 double diameter = 0;

 diameter = double.Parse(radius.Text) * double.Parse(radius.Text);

Now after the equation, you can assign the value of “diameter” to lblDiameter, but you have to convert it to String:

lblDiameter.Text = diameter.ToString();

If you prefer, you can use only 2 lines of codes like below:

 double diameter = double.Parse(radius.Text) * double.Parse(radius.Text);
 lblDiameter.Text = diameter.ToString();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x