Global Variable Issue I Think? (calculations)

squaremation

Active Member
Licensed User
Longtime User
I am working on a Concrete & Dirt Calculator. I keep getting 0.0 in all the output labels in final layout. This could also be a issue w/the radio buttons?
There are 4 modules:
Main (just instructions begin button)

InputSet (calculator ui for user to input length & width values stored in Globals)

HeightSet (8 radio buttons for user to select a hight value stored in Globals)

Calculated (User to click calculate button and fields return result based on calulations using Globals)



Process_Globals
B4X:
   Dim inputLength As Double
   Dim inputHeight As Double
   Dim inputWidth  As Double
   Dim calulation  As Double
   Dim setLayout As Int
   Dim reset  
   Dim Total As Double

(InputSet Module) Calculation after user inters length & height
B4X:
Sub btnAction_Click
   Dim numb1 As Int
   Dim numb2 As Int 
'sets the transition between inputs length, width, height, and final calcualtion
   If setLayout = 1 Then 
      numb1 = lblResult.Text
      inputWidth = numb1   
      lblComments.Text = "Enter project width" & CRLF & "and click on OK"
      setLayout = 2
      
        lblResult.Text = ""
   Else If setLayout = 2 Then
      numb2 = lblResult.Text
      inputLength = numb2
      lblComments.Text = "Enter project length" & CRLF & "and click on OK"
      setLayout = 3
        lblResult.Text = ""
   End If
   
   If setLayout = 3 Then
      StartActivity("HeightSet")
   End If

End Sub

(HeightSet Module) Sub Globals
B4X:
   Dim RadioButton1 As RadioButton
   Dim RadioButton2 As RadioButton
   Dim RadioButton3 As RadioButton
   Dim RadioButton4 As RadioButton
   Dim RadioButton5 As RadioButton
   Dim RadioButton6 As RadioButton
   Dim RadioButton7 As RadioButton
   Dim RadioButton8 As RadioButton
   Dim lblResult As Label
   Dim lblComments As Label
   Dim num1, num2, num3, num4, num5, num6, num7, num8 As Double

User picks height by Radio Button
B4X:
Sub RadioButtion1_CheckedChange(Chkd As Boolean)
   Dim inputHeight As Double 
   If Chkd Then
      num1 = .0833
      inputHeight = num1
   End If
End Sub

Sub RadioButtion2_CheckedChange(Chkd As Boolean)
   If Chkd Then
      num2 = .0666
      inputHeight = num2
   End If
[COLOR="Lime"]'and so on for 8 radio buttons[/COLOR]
End Sub


End Sub

(Calculate Module) Sub_Globals
B4X:
   Dim Label1 As Label
   Dim Label2 As Label
   Dim Label3 As Label
   Dim lblExtraYards As Label
   Dim lblExtraEight As Label
   Dim lblExtraSix As Label
   Dim btnNewCalc As Button 
   Dim yrds, sixty, eighty, yrdsExtra, sixtyExtra, eightyExtra As Double
   End Sub

Calculation/Exit
B4X:
Sub btnNewCalc_Click ()
   If btnNewCalc.text  = "Calculate" Then
 
      Total = inputLength * inputWidth * inputHeight
   
      'yards calculation
      Total = yrds
      Label1.Text = yrds 
   
      '60 lb bag calculation
      sixty = Total * .017
      Label2.Text = sixty
   
      '80 lb bag calculation
      eighty = Total * 45
      Label3.Text = eighty
   
      'set yard extra
      yrdsExtra = (Total * .15) + Total
      'lblExtraYards.Text = yrdsExtra
   
      'set extra % 60lb
      sixtyExtra = (sixty * .15) + Total
      lblExtraSix.Text = sixtyExtra
      
      'set extra % 80lb
      eightyExtra = (eighty * .15) + Total
      lblExtraEight.Text = eightyExtra
   
      btnNewCalc.Text = "New Calculation"
   
   Else 
       inputWidth = 0
       inputLength = 0
       inputHeight = 0
   StartActivity("InputSet")
   End If
End Sub
Sub btnExit_Click
    inputWidth = 0
    inputLength = 0
    inputHeight = 0
   ExitApplication
End Sub

Not sure what I'm doing wrong:sign0104:
 

Attachments

  • Concret Calc.zip
    13.3 KB · Views: 175

admac231

Active Member
Licensed User
Longtime User
Okay, what you're doing is redeclaring all these
B4X:
   Dim inputLength As Double
    Dim inputHeight As Double
    Dim inputWidth  As Double
    Dim calulation  As Double
    Dim setLayout As Int
    Dim reset  
    Dim Total As Double
in every activity. So when you go from Main to Calculated to InputSet they are all getting redeclared and thus set to 0.

What you should do is keep the above code in the Main activity only then reference them from other activities i.e (in the Calculated activity):
B4X:
Sub btnNewCalc_Click ()
   If btnNewCalc.text  = "Calculate" Then 
      Total = main.inputLength * main.inputWidth * main.inputHeight
 
Upvote 0

squaremation

Active Member
Licensed User
Longtime User
Ok I changed the Globals to only be declared in the Main module and then set all variable refs like " main.myvariable ". Changed Total to a local.

But still get 0.0 returned in all fields after calculation. :(

Updated Zip attached
 

Attachments

  • Concret Calc2.zip
    13.1 KB · Views: 165
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
Squaremation,
Main problem seems to be in Module CALCULATED -
Line 41 you set Total = ..................
Line 44 you set Total=yards when yards has not been set
Line 45 you set Label1.Text = yrds - hence the zero result

Changing line 44 to
yards = Total
seems to solve the problem.

Also I think there is a logic error in line 52 -> eighty = Total * 45 ,
which gives an awful lot of 80lb bags!

Perhaps once you've got the basic app working, a metric option would make it more versatile (I've just done such a calculation resulting in 14 cubic metres [494 cubic feet] of concrete!)

Cheers,
Douglas
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I had also a look at your project.
Besides the errors reported by DouglasNYoung there is another one.
You have 8 RadioButtons (RadioButton1 ...) but the Eventroutinename is RadioButtion1:CheckedChange.
None of these routines is ever called so the Main.inputHeight variable is always equal to 0, and Total is also always equal to 0.

From a users point of view I would prefer having all information on one screen, data and results. And the minimum possible screen touches to enter the data and get the result.
The first screen with only the Begin button is not necessary and will become boring to the user when he often runs the program.
I would use a spinner for the height instead of 8 radiobuttons on the same screen as the other input data. But these are just my thoughts.

Attached you find a modified working version.
I replaced the 8 RadioButton routines by one.

Best regards.
 

Attachments

  • Concret Calc3.zip
    13 KB · Views: 175
Upvote 0

squaremation

Active Member
Licensed User
Longtime User
solved thns douglas, adMac, klaus

Thnx Douglas, adMac, and Klaus finally a got it working!:sign0098:

DouglasNYoung
Also I think there is a logic error in line 52 -> eighty = Total * 45 ,
which gives an awful lot of 80lb bags!

I know it seems incorrect, but it takes 45 80lb bags or 60 60lb bags to = 1 cubic yard. Have to go with the cubic yard because if you don't buy bags and order a truck load pre -mixed the delivery system here in US is based on the cubic yard. In the 'Total' equation I did find a error though , forgot to divide by 27 should have been:

B4X:
Total =(main.inputLength * main.inputWidth * main.inputHeight) /27

klaus
The first screen with only the Begin button is not necessary and will become boring to the user when he often runs the program.
I would use a spinner for the height instead of 8 radiobuttons on the same screen as the other input data. But these are just my thoughts.
I agree change to all inputs in single module w/ 'Height' on a spinner selection.
Begin screen will be to chose calculation, will have Drywall, Paint, and Dirt as selections to calculate once finnished

Thnx Again U 3 :sign0188: will share finnal product!
 
Upvote 0
Top