Reputation: 95
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?
Upvotes: 3
Views: 22609
Reputation: 2201
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();
Upvotes: 0
Reputation: 39
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;
Upvotes: 1
Reputation: 22133
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? ;)
Upvotes: 4