4 Replies - 2293 Views - Last Post: 17 March 2013 - 01:15 PM Rate Topic: -----

#1 CelticKitty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 13

Where to place an accumulator?

Posted 17 March 2013 - 12:23 PM

I am very new to VB.NET and coding in general, and I have a question. I need to put a loop in my code to keep a running total of the Cost to run these items. I have put several appliances in a ComboBox, and once a washer is chosen, the cost is adjusted to include the cost of the water. However, I do not know how to get the cost of the appliances to be totaled together.

I have tried several different loops, but it ultimately comes down to it not being able to get the total from inside the IF...Else conditions. In full disclosure, this is an assignment, but I am not asking you to do it for me. Just to please help me understand how I can get a loop to pull information out of IF...Else and add it to an accumulator. Am I just trying to make this more complicated that it needs to be?

Could someone please steer me in the right direction?

Thank you in advance for your assistance.

 
Public Class frmMain

    Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'Loads the Main form


        lblCostGallon.Hide() 'Hides the Cost per Gallon label for the Washer Selection
        lblGallons.Hide() 'Hides the Gallon label for the Washer Selection
        txtCostGallons.Hide() 'Hides the Cost per Gallon text box for the Washer Selection
        txtGallons.Hide() 'Hides the Gallon text box for the Washer Selection



    End Sub
    

    Function TotalPrice(ByVal decCost As Decimal, ByVal decPower As Decimal, dblHours As Double) As Decimal 'This function calculates the total price of all selections but the Washer by passing the values of the Hour, Power, and Cost variables

        Dim decTotalPrice As Decimal 'Sets the Total Price as a Decimal

        decTotalPrice = CDec(decCost * decPower * dblHours) 'Calculates the total price, multiplying the cost, power, and hours and converting it to a decimal

        Return decTotalPrice 'Returns the value of the Total Price

    End Function 'Ends the Total Price function


    Function WasherPrice(ByVal decCost As Decimal, ByVal decPower As Decimal, dblHours As Double, ByVal decCostGallon As Decimal, ByVal dblGallons As Double) As Decimal 'This function calculates the total price of the Washer by passing the values of the Hour, Power, Cost, CostGallons, and Gallons variables

        Dim decWasherTotal As Decimal 'Sets the WasherTotal as a Decimal

        decWasherTotal = CDec(decCost * decPower * dblHours) + CDec(decCostGallon * dblGallons) 'Calculates the Washer's total price, multiplying the cost, power, and hours, multiplying the CostGallons and Gallons, and then adding the two values and converting it to a decimal

        Return decWasherTotal 'Returns the falue of the WasherTotal

    End Function 'Ends the WasherPrice function


    Private Sub txtCost_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCost.KeyPress, txtCostGallons.KeyPress, txtGallons.KeyPress, txtHours.KeyPress, txtPower.KeyPress 'Checks to see if any of the TextBoxes have data entered or changed

        If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) And e.KeyChar <> Chr(46) Then 'If the keys pressed are 0-9, the Backspace key, or the Decimal Point

            e.Handled = True 'Then let them be displayed
        End If

    End Sub 'End keypress validation


    Private Sub cbxAppliance_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbxAppliance.SelectedIndexChanged, txtHours.TextChanged, txtCost.TextChanged, txtPower.TextChanged, txtCostGallons.TextChanged, txtGallons.TextChanged 'When any of the fields are changed






        If cbxAppliance.SelectedItem Is "Washer" Then 'If the washer is selected from the combobox
            lblCostGallon.Show() 'Show the Cost per Gallon label 
            lblGallons.Show() 'Show the number of Gallons label 
            txtCostGallons.Show() 'Show the cost per gallon text box (cost per gallon of water)
            txtGallons.Show() 'Show the number of gallons text box (number of gallons needed)
            Dim decCost As Decimal 'Declare the cost variable as a decimal (cost per kW hour)
            Dim decPower As Decimal 'Declare the power variable as a decimal (number of kWs needed)
            Dim dblHours As Double 'Declare the hours variable as a double (how long the appliance will be run)
            Dim decWasherPrice As Decimal 'Declare the WasherPrice as a Decimal (the total price to run the washer)
            Dim decCostGallon As Decimal 'Declare the Cost per Gallon as a Decimal (cost per gallon)
            Dim dblGallons As Double 'Declare the Gallons variable as a Double (number of gallons)
            Dim strListOutput As String 'Declares the output of the ComboBox and places it in this variable 
            
            strListOutput = cbxAppliance.Text

            If Decimal.TryParse(txtCost.Text, decCost) Then 'Try to take the input of the Cost Text box, parse it, convert it to a Decimal, and house it in the decCost variable

                If Decimal.TryParse(txtPower.Text, decPower) Then 'If it is able to be done, then try to take the input from the Power Text box, parse it, convert it to a Decimal, and house it in the decPower variable

                    If Decimal.TryParse(txtCostGallons.Text, decCostGallon) Then 'If these two are able to be done, then try to take the input from the Cost per Gallon Text box, parse it, convert it to a Decimal, and house it in the decCostGallon variable

                        If Double.TryParse(txtGallons.Text, dblGallons) Then 'If these are still able to be done, then try to take the input from the Gallons Text box, parse it, convert it to a Double, and house it in the dblGallons variable


                            If Double.TryParse(txtHours.Text, dblHours) And dblHours <= 24 And dblHours > 0 Then 'If all the previous are still able to be done, then try to take the input from the Hours Text box, parse it, convert it to a Double, and house it in the dblHours variable.  Check to make sure the dblHours variable is greater than 0 and less than or equal to 24
                                decWasherPrice = WasherPrice(decCost, decPower, dblHours, decCostGallon, dblGallons) 'Calculate the price to run the washer by calling the Washer Price function


                                lblTotal.Text = "It costs " & decWasherPrice.ToString("c") & " to operate the " & cbxAppliance.Text & "." 'Display the price message in the Total label.

                                lstOutput.Items.Add("-----------------------------------------------------------------------------------------------------")
                                lstOutput.Items.Add("The selected appliance is: " + strListOutput + ".")
                                lstOutput.Items.Add("The number of kiloWatts of power it takes to run this appliance is: " + txtPower.Text + ".")
                                lstOutput.Items.Add("The time the " + strListOutput + " is/was/will be used: " + txtHours.Text + " hour(s).")
                                lstOutput.Items.Add("It costs " + decCost.ToString("c") + " per kiloWatt hour to run this appliance.")
                                lstOutput.Items.Add("This appliance uses " + txtGallons.Text + " gallon(s) of water.")
                                lstOutput.Items.Add("It costs " + decCostGallon.ToString("c") + " per gallon of water.")
                                lstOutput.Items.Add("The total cost to run this appliance is " + decWasherPrice.ToString("c") + " dollar(s).")
                                lstOutput.Items.Add("-----------------------------------------------------------------------------------------------------")

                            Else 'If the number of hours is outside the range
                                MessageBox.Show("Enter a numeric value for the nubmer of hours greater than 0 and less than 24.") 'Display this message
                                txtHours.Focus() 'Return the focus to the hours text box

                            End If 'End Hour Validation

                        End If 'End Gallon Validation

                    End If 'End Cost of Gallon Validation

                End If 'End Power validation

            End If 'End Cost validation


        Else 'If any other appliance is chosen besides the Washer

            lblCostGallon.Hide() 'Hide the Cost per Gallon label
            lblGallons.Hide() 'Hide the Number of Gallons label
            txtCostGallons.Hide() 'Hide the text box for the cost per gallon
            txtGallons.Hide() 'Hide the text box for the nubmer of gallons

            Dim decCost As Decimal 'Declare the variable decCost as a Decimal (the cost per kW hour)
            Dim decPower As Decimal 'Declare the variable decPower as a Decimal (the number of kW needed)
            Dim dblHours As Double 'Declare the variable dblHours as a Double (how long the appliance is run)
            Dim decTotalPrice As Decimal 'Declare the total price as a Decimal (the total cost to run the appliance)
            Dim strListOutput As String 'Declares the output of the ComboBox and places it in this variable
            
            strListOutput = cbxAppliance.Text

            If Decimal.TryParse(txtCost.Text, decCost) Then 'Try to take the input of the Cost Text box, parse it, convert it to a Decimal, and house it in the decCost variable

                If Decimal.TryParse(txtPower.Text, decPower) Then 'Try to take the input of the Power Text box, parse it, convert it to a Decimal, and house it in the decPower variable

                    If Double.TryParse(txtHours.Text, dblHours) And dblHours <= 24 And dblHours > 0 Then 'Try to take the input of the Hours Text box, parse it, convert it to a Double, and house it in the decHours variable. Test the variable to make sure it is within range.
                        decTotalPrice = TotalPrice(decCost, decPower, dblHours) 'Calculate the total price by calling the TotalPrice function and set the value as the decTotalPrice variable
                        lblTotal.Text = "It costs " & decTotalPrice.ToString("c") & " to operate the " & cbxAppliance.Text & "." 'Display the total price in the Total label

                        lstOutput.Items.Add("-----------------------------------------------------------------------------------------------------")
                        lstOutput.Items.Add("The selected appliance is: " + strListOutput + ".")
                        lstOutput.Items.Add("The number of kiloWatts of power it takes to run this appliance is: " + txtPower.Text + ".")
                        lstOutput.Items.Add("The time the " + strListOutput + " is/was/will be used: " + txtHours.Text + " hour(s).")
                        lstOutput.Items.Add("It costs " + decCost.ToString("c") + " per kiloWatt hour to run this appliance.")
                        lstOutput.Items.Add("The total cost to run this appliance is " + decTotalPrice.ToString("c") + " dollar(s).")
                        lstOutput.Items.Add("-----------------------------------------------------------------------------------------------------")


                    Else 'If dblHours is outside the range
                        MessageBox.Show("Enter a numeric value for the nubmer of hours greater than 0 and less than 24.") 'display this message
                        txtHours.Focus() 'Refocus the cursor to the txtHours box

                    End If 'End Hour validation

                End If 'End Power validation

            End If 'End Cost validation

        End If 'End combobox condition



    End Sub 'End Appliance routine

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click 'When the Reset button is clicked
        txtCost.Text = "" 'The TextBox for the Cost is cleared
        txtHours.Text = "" 'The TextBox for the Hours is cleared
        txtCostGallons.Text = "" 'The TextBox for the Cost per Gallons is cleared
        txtPower.Text = "" 'The TextBox for the Power is cleared
        txtGallons.Text = "" 'The TextBox for the Gallons is cleared
        lblTotal.Text = "" 'The label for the Total is cleared
        lstOutput.Items.Clear() 'The Listbox is cleared
        cbxAppliance.SelectedIndex = -1 'The combobox is cleared
        cbxAppliance.Focus() 'The focus is returned to the combobox

    End Sub 'End subroutine


    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click 'When the Exit button is clicked

        Me.Close() 'The application closes

    End Sub 'End of Exit button Click handler


    
End Class 'End Main class



Is This A Good Question/Topic? 0
  • +

Replies To: Where to place an accumulator?

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3467
  • View blog
  • Posts: 11,767
  • Joined: 12-December 12

Re: Where to place an accumulator?

Posted 17 March 2013 - 12:36 PM

I haven't read your assignment in any detail but I presume you need a private member variable:

Public Class Form1
    Private total As Double = 0.0

This variable can be referred to, and modified, by any of your forms' methods. So you can add to it at appropriate points and possibly create a method which would display its current value, and perhaps reset it to 0.0 at some stage.

Alternatively, based on your initial description, you could loop through your combobox items, adding to this total. This could happen in one go, rather than accumulating the total as items are added.
Was This Post Helpful? 0
  • +
  • -

#3 CelticKitty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 13

Re: Where to place an accumulator?

Posted 17 March 2013 - 01:02 PM

Thank you, Andrew for your speedy reply! Basically, the user chooses an option from the combobox, enters the data, and the total is calculated and displayed (say this is the first choice). If the user picks a different option from the combobox, enters new data, a new total is calculated based on the new information. I need to take the total from the first choice and keep adding any new totals for a running Grand Total until the user either resets the form or ends the program. I hope this provides more information that helps.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3467
  • View blog
  • Posts: 11,767
  • Joined: 12-December 12

Re: Where to place an accumulator?

Posted 17 March 2013 - 01:07 PM

View PostCelticKitty, on 17 March 2013 - 01:02 PM, said:

Thank you, Andrew for your speedy reply! Basically, the user chooses an option from the combobox, enters the data, and the total is calculated and displayed (say this is the first choice). If the user picks a different option from the combobox, enters new data, a new total is calculated based on the new information. I need to take the total from the first choice and keep adding any new totals for a running Grand Total until the user either resets the form or ends the program. I hope this provides more information that helps.

Well, yes, this explains a little more; but I have already suggested how you might maintain a total - it's up to you to implement it.

This post has been edited by andrewsw: 17 March 2013 - 01:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 CelticKitty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 13

Re: Where to place an accumulator?

Posted 17 March 2013 - 01:15 PM

Thank you very much :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1