btnCalculate Code. HELP!!!!!!!!!!!!!!!!!!!!!!!!!

TheRookie

Member
Joined
Sep 15, 2005
Messages
9
Programming Experience
Beginner
btnCalculate Code. HELP!!!!!!!!!!!!!!!!!!!!!!!!!RESOLVED

I'm working on a project in class. I've done most everything else, but now i'm stuck on coding the btnCalculate_Click. I've got to now:

a) Convert (Length) field value from text to double (numeric)
b) Call a function I created named 'IsPositiveDouble'
if ok - go on
if not, messagebox
c) Do the same for Width and Height field values
d) Call a function I created called Volume (length*width*height)
return Double
e) Convert Volume to text
f) Display volume, formatted

This is my first programming class, and it's kicking my #^@^*&%*&%!!

Please help!!!!
 
Last edited:
Hey,

You can use something like:

VB.NET:
dim LengthDbl, WidthDbl, HeightDbl, VolumeDbl as Double
 
LengthDbl = System.Convert.ToDouble(LengthString)
WidthDbl = System.Convert.ToDouble(WidthString)
HeightDbl = System.Convert.ToDouble(HeightString)
 
if Not IsPositiveDouble(LengthDbl) then
MsgBox("Not a valid number")
Exit Sub
end if
 
if Not IsPositiveDouble(WidthDbl) then
MsgBox("Not a valid number")
Exit Sub
end if
 
if Not IsPositiveDouble(HeightDbl) then
MsgBox("Not a valid number")
Exit Sub
end if
 
VolumeDbl = Volume(LengthDbl, WidthDbl, HeightDbl)
 
VolumeString = VolumeDbl.ToString
 
i recamend avoiding using the Exit statements (unless it's absolutely needed)

here's what i propose:
VB.NET:
Dim LengthDbl, WidthDbl, HeightDbl, VolumeDbl as Double
Dim blnErrorFree As Boolean = True

Try 
  LengthDbl = System.Convert.ToDouble(LengthString)
  WidthDbl = System.Convert.ToDouble(WidthString)
  HeightDbl = System.Convert.ToDouble(HeightString)
Catch ex As Exception
  blnErrorFree = False
  Messagebox.Show("Invalid Number" & ControlChars.NewLine & ex.Message)
End Try
If blnErrorFree = True Then 
  VolumeDbl = Volume(LengthDbl, WidthDbl, HeightDbl)
  VolumeString = VolumeDbl.ToString
End If

note that the Try/Catch block will catch any conversion errors and change the boolean value so other invalid operations cant be made
 
Oh I see... Diss my code will ya.. *Sticks chest out and makes huffing sounds*,

:p

Seriously though I conceed to JB's suggestions, much more elegant and complete.
As a quick Q (JB), I've never felt very comfortable using Exit statements due to similar comments in the past, but do nonetheless in some situations. Why are they bad news?
 
BadgerByte said:
Oh I see... Diss my code will ya.. *Sticks chest out and makes huffing sounds*,

:p

Seriously though I conceed to JB's suggestions, much more elegant and complete.
As a quick Q (JB), I've never felt very comfortable using Exit statements due to similar comments in the past, but do nonetheless in some situations. Why are they bad news?

lol (i needed that laugh, thanx :) )

it's not that exit statements are 100% bad, but i've always been told by various programmers that Exit statements confuse the logic flow and in most cases makes it much more difficult for someone (who didnt write it in the first place) to read and understand what the code is doing

the more confused the person is, the less work that gets done

but i've also been told from time to time that Exit statements are ok to use in some scenarios but i cant think of one right off hand, i've also never had a need to use one myself either
 
JB, Badger,

The code works, but now when I hit the 'Calculate' button, the result goes into the 'Volume' label box, instead of the 'lblVolumeTotal' box, where it should go...
 
Is this where the problem is?:

pdblVolumeTotal = Volume(txtLength.Text, txtWidth.Text, txtHeight.Text)
lblVolume.Text = System.Convert.ToString (pdblVolumeTotal)
 
yup, lblVolume.Text = pdblVolumeTotal.ToString would also work

so the last IF statment would look something like this:
VB.NET:
If blnErrorFree = True Then 
  VolumeDbl = Volume(LengthDbl, WidthDbl, HeightDbl)
  lblVolume.Text = VolumeDbl.ToString
'or
'lblVolume.Text = pdblVolumeTotal.ToString
'Depending on what variables you used
End If
 
Back
Top