Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Dim Leakage As Double Dim Leakdistance As Double Dim SideScatter As Double Dim SideScatterdistance As Double Dim LeakageSideScatter As Double Dim LeakageSideScatterdistance As Double Dim ForwardBackScatter As Double Dim ForwardBackScatterdistance As Double Dim LeakageForwardBackScatter As Double Dim LeakageForwardBackScatterdistance As Double Dim weeklysecondaryunshielded As Double Dim Leakvalue As Double Dim Sidescattervalue As Double Dim LeakageSideScattervalue As Double Dim ForwardBackScattervalue As Double Dim LeakageForwardBackScattervalue As Double Dim patients As Double patients = Convert.ToDouble(txtpatients.Text) If txtLeak.Text = "" Then Leakage = 0 Else Leakdistance = Convert.ToDouble(txtleakdistance.Text) Leakage = Leakvalue / (Leakdistance ^ 2) Leakvalue = Convert.ToDouble(txtLeak.Text) End If weeklysecondaryunshielded += Leakage If txtSidescatter.Text = "" Then SideScatter = 0 Else SideScatterdistance = Convert.ToDouble(txtSideScatterdistance.Text) SideScatter = (Sidescattervalue * patients) / (SideScatterdistance ^ 2) Sidescattervalue = Convert.ToDouble(txtSidescatter.Text) End If weeklysecondaryunshielded += SideScatter If txtLeakageSideScatter.Text = "" Then LeakageSideScatter = 0 Else LeakageSideScatterdistance = Convert.ToDouble(txtleakscatterdistance.Text) LeakageSideScatter = (LeakageSideScattervalue * patients) / (LeakageSideScatterdistance ^ 2) LeakageSideScattervalue = Convert.ToDouble(txtLeakageSideScatter.Text) End If weeklysecondaryunshielded += LeakageSideScatter If txtForwardBackscatter.Text = "" Then ForwardBackScatter = 0 Else ForwardBackScatterdistance = Convert.ToDouble(txtforwardbackscatterdistance.Text) ForwardBackScatter = (ForwardBackScattervalue * patients) / (ForwardBackScatterdistance ^ 2) ForwardBackScattervalue = Convert.ToDouble(txtForwardBackscatter.Text) End If weeklysecondaryunshielded += ForwardBackScatter If txtLeakageForward.Text = "" Then LeakageForwardBackScatter = 0 Else LeakageForwardBackScatterdistance = Convert.ToDouble(txtleakageforwardbackscatterdistance.Text) LeakageForwardBackScatter = (LeakageForwardBackScattervalue * patients) / (LeakageForwardBackScatterdistance ^ 2) LeakageForwardBackScattervalue = Convert.ToDouble(txtLeakageForward.Text) End If weeklysecondaryunshielded += LeakageForwardBackScatter 'Calculate the weekly unshielded secondary air kerma txtweeklysecondaryunshielded.Text = weeklysecondaryunshielded.ToString("0.00") Dim Secondarytransmissionfactor As Double Dim Secondaryalphagamma, Secondaryxbarrier, SecondaryNTK, Secondarybetaalpha As Double Dim Secondaryalpha As Double, Secondarybeta As Double, Secondarygamma As Double Dim numpatients As Double, occupancy As Double, secondaryairkerma As Double Dim Secondarydesigngoal As Double, Secondarythicknessrequired As Double Secondaryalpha = Convert.ToDouble(txtalpha2.Text) Secondarybeta = Convert.ToDouble(txtbeta2.Text) Secondarygamma = Convert.ToDouble(txtgamma2.Text) numpatients = Convert.ToDouble(txtpatients.Text) occupancy = Convert.ToDouble(txtoccupancy.Text) secondaryairkerma = Convert.ToDouble(txtweeklysecondaryunshielded.Text) Secondarydesigngoal = Convert.ToDouble(txtDesigngoal.Text) 'Calculate the transmission factor required for the primary barriers Secondarytransmissionfactor = (Secondarydesigngoal / occupancy) / secondaryairkerma txtSecondarytransmissionfactor.Text = Secondarytransmissionfactor.ToString("#.0E+0") 'Calculate the thickness of the lead needed to shield the dedicated chest unit Secondaryalphagamma = 1 / (Secondaryalpha * Secondarygamma) SecondaryNTK = ((weeklysecondaryunshielded * occupancy) / Secondarydesigngoal) ^ Secondarygamma Secondarybetaalpha = Secondarybeta / Secondaryalpha Secondaryxbarrier = (SecondaryNTK + Secondarybetaalpha) / (1 + Secondarybetaalpha) ' Dim calc As Double Secondarythicknessrequired = (Secondaryalphagamma) * Math.Log(Secondaryxbarrier) TxtSecondarythickness.Text = Secondarythicknessrequired.ToString("#.0")
Calculation does not add up
Page 1 of 17 Replies  948 Views  Last Post: 20 March 2013  12:02 PM
#1
Calculation does not add up
Posted 20 March 2013  07:55 AM
I have not found any error yet in my code but my calculation is giving me a wrong answer. I have gone through this code over and over to rectify my mistake but still can't get it right. I would appreciate if someone could point out where in my code that there is no logic.
Replies To: Calculation does not add up
#2
Re: Calculation does not add up
Posted 20 March 2013  08:05 AM
It would be helpful to know what you were entering, what you got out, and what you were expecting to get out.
#3
Re: Calculation does not add up
Posted 20 March 2013  08:18 AM
Below are entries: The unshielded secondary air kerma should be = 0.18, the transmission factor should be = 0.11, and the thickness required should be 0.42.
Attached image(s)
#4
Re: Calculation does not add up
Posted 20 March 2013  08:46 AM
Hi koppong,
It would help more if we new the names of the textboxes that have the numbers in them. I can`t tell what textbox name in your code contains (300) and which contains (.0027) and so on. List the names of each textbox that has a number in it and what number goes in it. Like :
textboxname = 300
textboxname = 0.0027
for each one that is to be set before the button6 is pressed. That would maybe help someone to see the problem.
Also i don`t know if you knew it or not but, using (#.0) in this line will round the number off. I don`t know if that was your intention.
It would help more if we new the names of the textboxes that have the numbers in them. I can`t tell what textbox name in your code contains (300) and which contains (.0027) and so on. List the names of each textbox that has a number in it and what number goes in it. Like :
textboxname = 300
textboxname = 0.0027
for each one that is to be set before the button6 is pressed. That would maybe help someone to see the problem.
Also i don`t know if you knew it or not but, using (#.0) in this line will round the number off. I don`t know if that was your intention.
TxtSecondarythickness.Text = Secondarythicknessrequired.ToString("#.0")
#5
Re: Calculation does not add up
Posted 20 March 2013  10:01 AM
Hello IronRazor,I intend to round it to one decimal, hence, the code ("#.0").
Below are txtboxes with their values:
Below are txtboxes with their values:
txtpatients.Text = 300 txtLeakageSideScatter.Text = 0.0027 txtleakscatterdistance.Text = 2.1 txtDesigngoal.Text = 0.02 txtoccupancy.Text = 1 txtalpha2.Text = 2.288 txtbeta2.Text = 9.848 txtgamma2.Text = 1.054 txtweeklysecondaryunshielded.Text (should be) = 0.18 txtSecondarytransmissionfactor.Text (should be) = 0.11 TxtSecondarythickness.Text (should be) = 0.42
This post has been edited by AdamSpeight2008: 20 March 2013  10:20 AM
#6
Re: Calculation does not add up
Posted 20 March 2013  11:31 AM
Hey,
I have changed a few things such as all the (Convert.ToDouble) i changed to (CDbl) which does the same thing but, uses less code space. I Put in messageboxes after each calculation so you can figure it on paper what each variable should be and then each time a messagebox pops up you can compare it to see where the problem is. I have a feeling it is in the (Secondarytransmissionfactor) calculation. Copy this code and replace the code in your button6 click event and it should help find the problem.
I have changed a few things such as all the (Convert.ToDouble) i changed to (CDbl) which does the same thing but, uses less code space. I Put in messageboxes after each calculation so you can figure it on paper what each variable should be and then each time a messagebox pops up you can compare it to see where the problem is. I have a feeling it is in the (Secondarytransmissionfactor) calculation. Copy this code and replace the code in your button6 click event and it should help find the problem.
Dim Leakage As Double Dim Leakdistance As Double Dim SideScatter As Double Dim SideScatterdistance As Double Dim LeakageSideScatter As Double Dim LeakageSideScatterdistance As Double Dim ForwardBackScatter As Double Dim ForwardBackScatterdistance As Double Dim LeakageForwardBackScatter As Double Dim LeakageForwardBackScatterdistance As Double Dim weeklysecondaryunshielded As Double Dim Leakvalue As Double Dim Sidescattervalue As Double Dim LeakageSideScattervalue As Double Dim ForwardBackScattervalue As Double Dim LeakageForwardBackScattervalue As Double Dim patients As Double patients = CDbl(txtpatients.Text) If txtLeak.Text = "" Then Leakage = 0 Else Leakdistance = CDbl(txtleakdistance.Text) Leakage = Leakvalue / (Leakdistance ^ 2) Leakvalue = CDbl(txtLeak.Text) End If weeklysecondaryunshielded += Leakage MessageBox.Show("weeklysecondaryunshielded = " & weeklysecondaryunshielded.ToString) If txtSidescatter.Text = "" Then SideScatter = 0 Else SideScatterdistance = CDbl(txtSideScatterdistance.Text) SideScatter = (Sidescattervalue * patients) / (SideScatterdistance ^ 2) Sidescattervalue = CDbl(txtSidescatter.Text) End If weeklysecondaryunshielded += SideScatter MessageBox.Show("weeklysecondaryunshielded = " & weeklysecondaryunshielded.ToString) If txtLeakageSideScatter.Text = "" Then LeakageSideScatter = 0 Else LeakageSideScatterdistance = CDbl(txtleakscatterdistance.Text) LeakageSideScatter = (LeakageSideScattervalue * patients) / (LeakageSideScatterdistance ^ 2) LeakageSideScattervalue = CDbl(txtLeakageSideScatter.Text) End If weeklysecondaryunshielded += LeakageSideScatter MessageBox.Show("weeklysecondaryunshielded = " & weeklysecondaryunshielded.ToString) If txtForwardBackscatter.Text = "" Then ForwardBackScatter = 0 Else ForwardBackScatterdistance = CDbl(txtforwardbackscatterdistance.Text) ForwardBackScatter = (ForwardBackScattervalue * patients) / (ForwardBackScatterdistance ^ 2) ForwardBackScattervalue = CDbl(txtForwardBackscatter.Text) End If weeklysecondaryunshielded += ForwardBackScatter MessageBox.Show("weeklysecondaryunshielded = " & weeklysecondaryunshielded.ToString) If txtLeakageForward.Text = "" Then LeakageForwardBackScatter = 0 Else LeakageForwardBackScatterdistance = CDbl(txtleakageforwardbackscatterdistance.Text) LeakageForwardBackScatter = (LeakageForwardBackScattervalue * patients) / (LeakageForwardBackScatterdistance ^ 2) LeakageForwardBackScattervalue = CDbl(txtLeakageForward.Text) End If weeklysecondaryunshielded += LeakageForwardBackScatter MessageBox.Show("weeklysecondaryunshielded = " & weeklysecondaryunshielded.ToString) 'Calculate the weekly unshielded secondary air kerma txtweeklysecondaryunshielded.Text = weeklysecondaryunshielded.ToString("0.00") Dim Secondarytransmissionfactor As Double Dim Secondaryalphagamma, Secondaryxbarrier, SecondaryNTK, Secondarybetaalpha As Double Dim Secondaryalpha As Double, Secondarybeta As Double, Secondarygamma As Double Dim numpatients As Double, occupancy As Double, secondaryairkerma As Double Dim Secondarydesigngoal As Double, Secondarythicknessrequired As Double Secondaryalpha = CDbl(txtalpha2.Text) Secondarybeta = CDbl(txtbeta2.Text) Secondarygamma = CDbl(txtgamma2.Text) numpatients = CDbl(txtpatients.Text) occupancy = CDbl(txtoccupancy.Text) secondaryairkerma = CDbl(txtweeklysecondaryunshielded.Text) Secondarydesigngoal = CDbl(txtDesigngoal.Text) 'Calculate the transmission factor required for the primary barriers Secondarytransmissionfactor = (Secondarydesigngoal / occupancy) / secondaryairkerma MessageBox.Show("Secondarytransmissionfactor = " & Secondarytransmissionfactor.ToString) txtSecondarytransmissionfactor.Text = Secondarytransmissionfactor.ToString("#.0E+0") 'Calculate the thickness of the lead needed to shield the dedicated chest unit Secondaryalphagamma = 1 / (Secondaryalpha * Secondarygamma) MessageBox.Show("Secondaryalphagamma = " & Secondaryalphagamma.ToString) SecondaryNTK = ((weeklysecondaryunshielded * occupancy) / Secondarydesigngoal) ^ Secondarygamma MessageBox.Show("SecondaryNTK = " & SecondaryNTK.ToString) Secondarybetaalpha = Secondarybeta / Secondaryalpha MessageBox.Show("Secondarybetaalpha = " & Secondarybetaalpha.ToString) Secondaryxbarrier = (SecondaryNTK + Secondarybetaalpha) / (1 + Secondarybetaalpha) MessageBox.Show("Secondaryxbarrier = " & Secondaryxbarrier.ToString) ' Dim calc As Double Secondarythicknessrequired = (Secondaryalphagamma) * Math.Log(Secondaryxbarrier) MessageBox.Show("Secondarythicknessrequired = " & Secondarythicknessrequired.ToString) TxtSecondarythickness.Text = Secondarythicknessrequired.ToString("#.0")
#8
Re: Calculation does not add up
Posted 20 March 2013  12:02 PM
modi123_1, on 20 March 2013  01:35 PM, said:
Just say'n  a decent dose of learning how to 'debug' goes a bit farther than slapping messageboxes everywhere. />/>
Yes i agree. That was told to him in a post relating to this same program before. />
Here is the Link to andrewsw `s debugging tutorial.
This post has been edited by IronRazer: 20 March 2013  12:08 PM
Page 1 of 1
