By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

How to write this simple program? converting to numbers???

P: n/a
Ron
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.

Here is what I am doing:
I have a textbox txtradius that I enter a radius into.

I then have a lable that I want the surface area to be displayed in
called lblsurface

here is my logic.

lblsurface.text = (4 * 3.14 * txtradius * txtradius)

I am getting a conversion error.
I dim txtradius as double
dim lblsurface as double

Thanks for any help

Jan 17 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Ron" <pt*****@yahoo.comwrote in news:1169051101.266111.300510@
51g2000cwl.googlegroups.com:
>
I then have a lable that I want the surface area to be displayed in
called lblsurface

here is my logic.

lblsurface.text = (4 * 3.14 * txtradius * txtradius)

I am getting a conversion error.
I dim txtradius as double
dim lblsurface as double
txtRadius is a textbox right?

In that case you should be doing:

Dim _Radius as Double = Ctype(txtRadiu.text, Double)
Jan 17 '07 #2

P: n/a
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.Text = nArea.ToString( "0.000" )

HTH,
Phill W.
Jan 17 '07 #3

P: n/a
Ron
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.Text = nArea.ToString( "0.000" )

HTH,
Phill W.
Jan 17 '07 #4

P: n/a
In the second line you put
CDbl(lblsurface.Text, double)
it should be
CDbl(lblsurface.Text)
or
CType(lblsurface.Text, double)
"Ron" <pt*****@yahoo.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.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.Text = nArea.ToString( "0.000" )

HTH,
Phill W.

Jan 17 '07 #5

P: n/a
First, I would use doubles, I would use decimals.

dim decSurface as decimal =0d
dim decRadius as decimal =0d
const decPi as decimal = 3.14d
dim decResult as decimal =0

if decimal.tryparse(lblsurface.text,decSurface) then
else
endif

and so on

decResult = (decRadius ^3) * decSurface * 4/3
"BillE" <be****@datamti.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
In the second line you put
CDbl(lblsurface.Text, double)
it should be
CDbl(lblsurface.Text)
or
CType(lblsurface.Text, double)
"Ron" <pt*****@yahoo.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.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.Text = nArea.ToString( "0.000" )

HTH,
Phill W.


Jan 17 '07 #6

P: n/a
AMDRIT wrote:
First, I would use doubles, I would use decimals.
.. . .
const decPi as decimal = 3.14d
Why use Decimals when all the Math methods return Doubles?
Why avoid using the Math library? The last time I looked, PI <3.14!

Regards,
Phill W.
Jan 18 '07 #7

P: n/a

Because

Dim dNumber1 As Decimal = Math.PI
Dim dNumber2 As Decimal = Math.PI ^ 2
Dim dNumber3 As Decimal = dNumber2 - dNumber1 + 5&
lblsurface.Text = dNumber3

is not the same as

Dim dNumber1 As Double = Math.PI
Dim dNumber2 As Double = Math.PI ^ 2
Dim dNumber3 As Double = dNumber2 - dNumber1
lblsurface.Text = dNumber3

and if you plan on reusing any of the numbers where they mean anything, then
I recommend decimals.

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:eo**********@south.jnrs.ja.net...
AMDRIT wrote:
>First, I would use doubles, I would use decimals.
. . .
>const decPi as decimal = 3.14d

Why use Decimals when all the Math methods return Doubles?
Why avoid using the Math library? The last time I looked, PI <3.14!

Regards,
Phill W.

Jan 18 '07 #8

P: n/a
My apologies, that second dNumber3 should have read Dim dNumber3 As Double =
dNumber2 - dNumber1 + 5&

"AMDRIT" <am****@hotmail.comwrote in message
news:Oq**************@TK2MSFTNGP06.phx.gbl...
>
Because

Dim dNumber1 As Decimal = Math.PI
Dim dNumber2 As Decimal = Math.PI ^ 2
Dim dNumber3 As Decimal = dNumber2 - dNumber1 + 5&
lblsurface.Text = dNumber3

is not the same as

Dim dNumber1 As Double = Math.PI
Dim dNumber2 As Double = Math.PI ^ 2
Dim dNumber3 As Double = dNumber2 - dNumber1
lblsurface.Text = dNumber3

and if you plan on reusing any of the numbers where they mean anything,
then I recommend decimals.

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:eo**********@south.jnrs.ja.net...
>AMDRIT wrote:
>>First, I would use doubles, I would use decimals.
. . .
>>const decPi as decimal = 3.14d

Why use Decimals when all the Math methods return Doubles?
Why avoid using the Math library? The last time I looked, PI <3.14!

Regards,
Phill W.


Jan 18 '07 #9

P: n/a
AMDRIT wrote:
Because

Dim dNumber1 As Decimal = Math.PI
Dim dNumber2 As Decimal = Math.PI ^ 2
Dim dNumber3 As Decimal = dNumber2 - dNumber1 + 5&
lblsurface.Text = dNumber3

is not the same as

Dim dNumber1 As Double = Math.PI
Dim dNumber2 As Double = Math.PI ^ 2
Dim dNumber3 As Double = dNumber2 - dNumber1
lblsurface.Text = dNumber3

and if you plan on reusing any of the numbers where they mean anything, then
I recommend decimals.
Strange, then, that Our Friends in Redmond seem satified with mere
Doubles for the Math methods... :-)

Regards,
Phill W.
Jan 18 '07 #10

P: n/a
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(txtRadius.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)))).ToString()

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).ToString()

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(txtRadius.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*********************@11g2000cwr.googlegrou ps.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.Text = nArea.ToString( "0.000" )

HTH,
Phill W.

Jan 18 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.