More Chapter 3 Method questions.
First a little background -
My first programming course punched cards for SYS370 in FORTRAN, and years (and years) ago, I did some TurboC. (in fact, for you Borland/CodeGear folks, Allan Bauer used to be my roomate and co-worker, but I could never get my brain around Pascal..) However, my career never really required me to do much
programming. I'm finding now that it's becomming more of a requirement to stay competative.
*SO*
Working on the Building a Method Try it Out on P.79. Worked ok as the book instructed, but I got the fancy idea of why not make the messagebox a sub as well, and why not make dblArea a public var? (cobbwebs getting brushed away in the back of my brain..).
So, I came up with this with AreaMessageBox() as a sub as well.
PublicClass Form1
'declare dblArea as public var
Dim dblArea AsDouble
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'calculate the area of a circle with radius of 100
dblArea = CalculateAreaFromRadius(100)
'display the results
AreaMessageBox(dblArea)
EndSub
PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Calculate the area of a circle with radius of 25
dblArea = CalculateAreaFromRadius(25)
'display the results
AreaMessageBox(dblArea)
EndSub
PrivateSub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'calculate area of circle with radius of 250
dblArea = CalculateAreaFromRadius(250)
'display the results
AreaMessageBox(dblArea)
EndSub
'Calculate area from radius - find the area of a circle
PrivateFunction CalculateAreaFromRadius(ByVal radius AsDouble) AsDouble
'declare variables
Dim dblRadiusSquared AsDouble
Dim dblResult AsDouble
'square the radius
dblRadiusSquared = radius * radius
'multiply by pi
dblResult = dblRadiusSquared * Math.PI
'return the result
Return dblResult
EndFunction
'Area Messagebox sub
PrivateFunction AreaMessageBox(ByRef dblArea AsDouble)
'display message box for the area calucations for each button click
MessageBox.Show("The area of the circle is " & dblArea.ToString, "Circle Area")
Return (dblArea)
EndFunction
EndClass
I pass dblArea to AreaMessageBox, and everything works. But WHY do I need to pass dblArea back as part of the return statement? I really don't need it. How can I get it to pass back a null or T/F of sub completion?
Thanks!
Allen
|