473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
10 1998
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.

Jan 18 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4703
by: Roy Riddex | last post by:
2nd post of the day! I'm just learning about Arrays at College and have met a problem. I have 5 text boxes for number input, a command button to add the numbers to the array, and a command button which displays the array contents in 5 labels. My program works fine. The problem is that I use a simple If statement to check for any empty boxes. I've to cut the IF statement and use a For Each...Next statement to check for empty text boxes but...
14
3059
by: Michael Levin | last post by:
I've got the following problem. I'm a biologist and I have a device at work which monitors my frog habitat. The device has a bunch of sensors, and runs an embedded html server with some java functions defined which know how to read the hardware sensorts. I access it from wherever I am via any browser, and it displays the measurements (a set of simple numbers) as Java applets in a simple html page. One entry in this page (a table cell) looks...
10
1413
by: sam | last post by:
hi, i am new to C/C++ and have just finished a program based on the UK national lottery, where you enter 6 numbers, six are generated by the computer and there are appropriate messages and a compare function to decide your winnings. I am using Microsoft's C++ version 6 standard, the program is as follows: /* * * * * * * * * * * * * * * * * * * * * * * Lottery Simulation By Sam...
0
1880
by: munif | last post by:
i wnat write a C++ program that would read from a CD and display information about files and folders in the given CD In simple words you would be performing a simple (ls -l) or (DIR /S) on the CD. LOOk at this code /*
8
12005
by: markus_fried | last post by:
Hi, we're using Access97 as frontend to a Oracle db. In Access we have a bound form which is using a Dynaset as source, global locking and form locking is off. All worked well until yesterday. We dublicated some records for a new session in the Oracle db and since then some of these records cannot be edited from Access. An error occurs: "This record has been changed by another user since you started editing...". We are in a single-user
18
1926
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point operations and disregard all integer operations. My question is this. I am writing a simulation for animal dispersement through large landscapes. We are loading data from several different files to simulate the environment the animals will be...
14
6492
by: MabZiCLe | last post by:
Hey guys, can you help me with this? C programming major in Turbo c. LOL Well, this is my problem. Create a program that accepts 10 positive integers. The program will group and sort all even and odd numbers. Even numbers will be sorted in ascending while odd numbers will be sorted in descending order. If theres is no even numbers found the program will print "NO even numbers" yet it prints the odd numbers, and sort it vice versa. Use...
2
2044
by: astrogirl77 | last post by:
Hi, I'm new to Python and am hoping to find help with coding a Python script, applet. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is to have all numbers added and...
1
2077
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.