I don't really know what you think you're doing with that bit of code but I
do think that you've lost the plot to some degree.
First of all, I'm going to shout this really loud because it's really
important:
TURN OPTION STRICT ON .. IT WILL SAVE YOU A LOT OF HEARTACHE
Now:
Dim _Radius As Double = CType(txtRadius .Text, Double)
This line is OK, but would be better written as:
Dim _Radius As Double = Double.Parse(tx tRadius.Text)
Although CType(..., Double) will, quite happily, cast a value of almost any
type that represents a Double as a Double, Double.Parse is specaily designed
for converting a String that represents a Double into a Double.
CDbl(lblsurface .Text, double) = 4 * 3.14 * (CDbl(txtRadius .Text) *
(CDbl(txtRadius .Text)))
This line makes me cringe. You are converting txtRadius.Text to a Double
twice, but what you think you are doing with the result of the math entirely
escapes me.
The result of the math needs to be converted to a string and assigned to the
label so it becomes:
lblsurface.Text = (4 * 3.14 * (CDbl(txtRadius .Text) *
(CDbl(txtRadius .Text)))).ToStr ing()
The multitude of parentheses is superfluous and an be removed:
lblsurface.Text = (4 * 3.14 * CDbl(txtRadius. Text) *
CDbl(txtRadius. Text)).ToString ()
A variable for the radius has already ben 'computed so you should be using
that instead:
lblsurface.Text = (4 * 3.14 * _Radius * _Radius).ToStri ng()
The nice epople at Microsoft have yet to remove the Exponation operator, (a
bit of sarcasm there), so use that instead. It has a higher precedence that
multiplication/division so there is still no need for any additional
parentheses:
lblsurface.Text = (4 * 3.14 * _Radius ^ 2).ToString()
At best, 3.14 is a very poor approxiamattion of PI and you want to be using
it only if you you were doing the calculation with pencil and papper, and
only then to save dealing with lots of decimal places. We have value for PI
with lots of precision so use that:
lblsurface.Text = (4 * Math.PI * _Radius ^ 2).ToString()
And again:
lblVolume.Text = (4 / 3 * 3.14 * (CDbl(txtRadius .Text) *
(CDbl(txtRadius .Text) * (CDbl(txtRadius .Text)))))
See the notes for the previous line:
lblVolume.Text = (4 / 3 * Math.PI * _Radius ^ 3).ToString()
So, to recap, the whole thing has become:
Dim _Radius As Double = Double.Parse(tx tRadius.Text)
lblsurface.Text = (4 * Math.PI * _Radius ^ 2).ToString()
lblVolume.Text = (4 / 3 * Math.PI * _Radius ^ 3).ToString()
Note that the code has now been made more efficient and, at the same time,
much more readable.
"Ron" <pt*****@yahoo. comwrote in message
news:11******** *************@1 1g2000cwr.googl egroups.com...
here is what I have and it does not work.
Dim _Radius As Double = CType(txtRadius .Text, Double)
CDbl(lblsurface .Text, double) = 4 * 3.14 *
(CDbl(txtRadius .Text) * (CDbl(txtRadius .Text)))
lblVolume.Text = (4 / 3 * 3.14 * (CDbl(txtRadius .Text) *
(CDbl(txtRadius .Text) * (CDbl(txtRadius .Text)))))
still not working.
Phill W. wrote:
>Ron wrote:
I want to calculate the surface area of a sphere from an inputed radius
with option strict on. I guess I am not converting something
correctly.
lblsurface.text = (4 * 3.14 * txtradius * txtradius)
You could boil all this down into a single statement, but here's the
long-hand way, so you can see all the conversions going on ...
Dim nRadius as Double = CDbl( txtRadius.Text )
Dim nArea as Double _
= 4 * Math.PI * Math.Pow( nRadius, 2 )
lblSurface.Tex t = nArea.ToString( "0.000" )
HTH,
Phill W.